August 2003
Intermediate to advanced
1104 pages
19h 27m
English
An alternative to if-elseif-else structures is the switch statement, which works on the assumption that you compare a single expression to a set of possible values. Figure 3.3 demonstrates the structure of a switch statement.
switch(root-expression) { case case-expression: default: } |
The root expression inside a switch statement is evaluated and then compared to each expression following a case statement. At the end of the list of cases you can put a default statement that works exactly like an else statement; it matches if no other case matches.
Notice that cases don't have curly braces after them. This reveals an important difference between if and switch. When an if block matches ...
Read now
Unlock full access