Branching and Iteration
The C language provides three types of decision-making constructs: if-else, the conditional ex-pression ?:, and the switch statement. It also provides three looping constructs: while, do-while, and for. And it has the infamous goto, which is capable of both non-conditional branching and looping.
3.1 If-Else
Basic if the statement examines a conditional expression and, if it is zero (i.e., true), then executes the latter statement for example, in this code segment
if (a < b)
b = a;
the assignment b = a will only occur if a is less-than b. The else statement deals with the alternative case where the conditional expression is 0 (i.e., FALSE).
if (a < b)
b = a;
else
b += 7;
The if-else statement can also command multiple statements by wrapping them in braces. State-ments so grouped are called a compound statement, or block, and they are syntactically equivalent to a single statement.
if (a < b) {
b = a;
a *= 2;
}
else {
b += 7;
--a;
}
It is possible to chain if-else statements if the following form
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
This chain is evaluated from the top and, if a particular if-conditional is TRUE, then its statement is executed and the chain is terminated. On the other hand, if the conditional is FALSE, the next if-conditional is tested. If all the conditionals evaluate to FALSE, then the final else statement is executed as a default. (Note, the final else is optional and, if it is missing, the default action is no action.)
An example if-else chain is shown below. This code segment performs integer division on the first k elements of an array of integers num[SIZE]. The first two if-statements do error-checking,1 and the final else does the actual calculation. Notice that the else is a compound statement, and that a variable (int i) is declared there; variables may be declared at the top of any block, and their scope is local to that block.
if (k < 0 || k > SIZE)
printf("Error: Invalid number of elements (out-of-bounds).\n"); else if (denom == 0)
printf("Error: Denominator is zero.\n");
else {
int i;
printf("Result of division by %d: ", denom);
for (i = 0; i < k; ++i)
printf("%d ", num[i]/denom);
printf("\n");
}
Note. A common mistake with if-else blocks is the “dangling else problem”. For example, consider the following nested-if statement.
if (a < b)
if (m != 0)
b = a;
else
a = m;
The intention of the programmer, as indicated by the indentation, is that the else corresponds to the outer if statement. However, it actually belongs to the inner if statement. Desired association can be enforced by brackets.
if (a < b) {
if (m != 0)
b = a;
}
else
a = m;
1 This code segment does not demonstrate good practice for performing error-checking—there are much better ways. Rather, it intends only to show a basic if-else chain.
3.2 ?: Conditional Expression
The conditional expression is a ternary operator; that is, it takes three operands. It has the following form
(expression 1) ? (expression 2) : (expression 3)
If the first expression is TRUE (i.e., non-zero), the second expression is evaluated, otherwise the third is evaluated. Thus, the result of the ternary expression will be the result of either the second or third expressions, respectively. For example, to calculate the maximum of two values,
c = (a > b) ? a : b; /* c = max(a,b) */
As a branching construct, the ?: operator appears far less frequently than the if-else and switch constructs.
No comments:
Post a Comment