switch (expression) { case value: instructions...
break; default: instructions...
break; }
LC
The
switch statement allows for branching on multiple values of a variable
or expression. The expression is evaluated and compared with the case values.
If it matches any of the case values,
the instructions following the colon are executed,
until a break statement is found.
Every case statement must be followed by instructions and a break statement; empty case statements are not allowed.
If the expression does not match any of the case statements,
and if there is a default statement, the instructions following default: are
executed, otherwise the switch statement ends.
Example:
var choice = 0;
...
switch(choice)
{
case 0:
printf("Zero! ");
break;
case 1:
printf("One! ");
break;
case 2:
printf("Two! ");
break;
default:
printf("None of them! ");
break;
}
See also:
if, while, goto, break, continue, comparisons
► latest
version online