By: Grenfel in C Tutorials on 2007年09月20日 [フレーム]
The if-else statement is used to express decisions. Formally the syntax isif (expression) statement1 else statement2where the else part is optional. The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement1 is executed. If it is false (expression is zero) and if there is an else part, statement2 is executed instead.
Since an if tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious is writing
if (expression)instead of
if (expression != 0)Sometimes this is natural and clear; at other times it can be cryptic.
Because the else part of an if-else is optional,there is an ambiguity when an else if omitted from a nested if sequence. This is resolved by associating the else with the closest previous else-less if. For example, in
if (n > 0) if (a > b) z = a; else z = b;the else goes to the inner if, as we have shown by indentation. If that isn't what you want, braces must be used to force the proper association:
if (n > 0) {
if (a > b)
z = a;
}
else
z = b;
The ambiguity is especially pernicious in situations like this:
if (n > 0)
for (i = 0; i < n; i++)
if (s[i] > 0) {
printf("...");
return i;
}
else /* WRONG */
printf("error -- n is negative\n");
The indentation shows unequivocally what you want, but the compiler doesn't get
the message, and associates the else with the inner if. This
kind of bug can be hard to find; it's a good idea to use braces when there are
nested ifs.
By the way, notice that there is a semicolon after z = a in
if (a > b) z = a; else z = b;This is because grammatically, a statement follows the if, and an expression statement like ``z = a;'' is always terminated by a semicolon.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in C )
Passing double value to a function in C
Sum of the elements of an array in C
Printing a simple histogram in C
Simple arithmetic calculations in C
Passing pointer to a function in C
Find square and square root for a given number in C
Using memset(), memcpy(), and memmove() in C
Latest Articles (in C)
Simple arithmetic calculations in C
Find square and square root for a given number in C
Printing a simple histogram in C
Sum of the elements of an array in C
Passing pointer to a function in C
Passing double value to a function in C
Infix to Prefix And Postfix in C
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate