switch (expression) { case value: instructions... default: instructions...
}
The
switch statement allows for branching on multiple values of an int 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. The execution continues
until either the closing bracket or a break statement
is encountered. 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:
int choice;
...
switch(choice)
{
case 0:
printf("Zero! ");
break;
case 1:
printf("One! ");
break;
case 2:
printf("Two! ");
break;
default:
printf("None of them! ");
}
See also:
if, while, break, continue, comparisons
► latest
version online