3.3 Switch
Switch Statement is a multi-way decision that tests whether an expression matches one of several continuous integer values, and accordingly the branches [KR88, page 58].
The general form of the switch statement is as follows.
switch (expression) {
case const-int-expr: statements
case const-int-expr: statements
default: statements
}
On evaluating the expression, if it matches one of the set of constant integer expressions, the switch branches to the matching case label and executes the statements following that point. Otherwise it jumps to the default label and executes its statements. The default label is optional, and if it does not exist, and none of the case labels match, the switch simply performs no action.
Note. The default label is typically the last label in the block. While this is good practice in general, it is not mandatory, and case labels may appear below default.
The statements following a case label are executed until terminated by a break statement, which causes an immediate exit from the switch block. However, if a break is not encountered, execution will flow on through to the next cases until the end of the block.2 This is termed fall through and is the default behaviour in a switch. Fall through is rarely used because it is di cult to code correctly; it should be used with caution.
Style Note. It is generally good practice to have a default label even when it is not necessary, even if it just contains an assert to catch logical errors (i.e., program bugs). Also, fall-through is much less common than break, and every case label should either end with a break or have a /* Fall Through */ comment to make ones intentions explicit. Finally, it is wise to put a break after the last case in the block, even though it is not logically necessary. Some day additional cases might be added to the end and this practice will prevent unexpected bugs.
2 Generally speaking, program execution will flow through, past the lower case labels, unless a branch out of the switch is encountered, such as break, return or goto.
It is worth mentioning here that all the control structures—if-else, ?:, while, do-while, and for—can be nested, which means that they can exist within other control statements. The switch-statement is no exception, and the statements following a case label may include a switch or other control structure. For example, the following code-structure is legitimate.
if (expression)
while (expression)
switch(integer expression) {
case A1:
switch(integer expression) {
case B1: statements
case B2: statements
case B3: statements
}
case A2: statements
default: statements
}
The following example converts the value of a double variable angle to normalised radians (i.e., −π ≤ angle ≤ π). The original angle representation is either degrees or radians, as indicated by the integer angletype, and DEG, RAD, and PI are symbolic constants.
switch (angletype)
{
case DEG:
angle *= PI / 180.0; /* convert to radians */ /* fall through */
case RAD:
while (angle > PI) /* normalise radians */
angle -= 2.0*PI;
while (angle < -PI)
angle += 2.0*PI;
break;
default:
printf("Error: Invalid type\n");
break;
}
3.4 While Loops
The while loop has the general form
while (expression)
statement;
If the conditional expression is TRUE, then the while will execute the following statement, after which it will reevaluate the conditional. It continues this iteration while ever the conditional remains TRUE. As with the if-else statement, the while loop can execute multiple statements as a block by enclosing them in braces.
For example, the following code segment computes the greatest common divisor (GCD) of two positive integers m and n (i.e., the maximum value that will divide both m and n). The loop iterates until the value of n becomes 0, at which point the GCD is the value of m.
while (n) {
int tmp = n;
n = m%n;
m = tmp;
}
No comments:
Post a Comment