Previous | Table of Contents | Next |
When a cascaded if/else chain repeatedly compares the same variable against a fixed set of targets, as in
if(x == 2) statement1; else if(x == 3) statement2; else if(x == 5 || x == 7) statement3; else statement4;
an alternate construction, the switch statement, is available. It looks like this:
switch(x) { case 2: statement1; break; case 3: statement2; break; case 5: case 7: statement3; break; default: statement4; break; }
Conceptually, the expression in the parentheses (here, the variable x) is evaluated once and compared to each of the case labels. The code corresponding to a match is executed. If there is no match, the default case is executed if it is present; otherwise, the entire switch statement is skipped, with none of the cases executed.
The keyword break marks the end of each case. These break statements are important: Without them, the interpretation is that execution should flow through from case to case. (This fall-through behavior can be a nuisance, but it is a traditional and now fixed part of the language.) The case labels may appear in any order. The default case need not be present, nor need it be last; regardless of its position, it always catches those values not matched by any of the explicit cases. None of the case labels may be duplicated. The case labels must all be compile-time constants. It is possible to attach multiple case labels to one block of code, as in the preceding example.
As the preceding discussion illustrates, the switch statement is somewhat limited. It can switch only on integral expressions, and the targets must all be explicitly listed. There is no way to trigger a match on a range of targets, except by listing them all explicitly. Nevertheless, for applications that do fall within the switch statements restrictions, it is a useful convenience. (As you might guess based on those restrictions, most compilers attempt to generate code corresponding to a jump table or the equivalent rather than perform a long chain of comparisons.)
Strictly speaking, the syntax of the switch statement is the keyword switch followed by a parenthesized expression followed by a single statement. In practice, the statement is invariably a brace-enclosed block. Like any block, it may contain local variables, but these are necessarily declared before the first case label and statement. Consequently, any initializers on those variables are never applied.
C has three kinds of loop constructions available, the simplest of which is the while loop. It is a test-at-the-top loop (analogous to Pascals while/do loop). The syntax is similar to an if statement:
while( condition ) statement
In fact, the action of a while loop is similar to an if statement as well, except that after each execution of the statement, the condition is tested again, and if it is still true, the condition is performed again. (If the condition is initially false, the body of the loop is not executed at all.) A classic example of a while loop is this one, which reads characters as long as the end-of-file marker EOF is not seen:
while((c = getchar()) != EOF) process character
(The expression is one that first appeared in section 3.3.4; it simultaneously assigns the character read by getchar to the variable c and compares it to the constant EOF.)
One way of writing an infinite loop (which either does not terminate at all, or does so only abnormally; see section 3.4.7) is to use a constant, true condition:
while(1)
Previous | Table of Contents | Next |