switch switch Statement
switch (expression) {
[declaration
...]
[case constant :]
[statement;
...]
...
[default:]
[statement;
...]
}
Switch provides a multi-point branch based on the value of
`expression'. `switch' evaluates `expression', then passes control
to the case constant that matches `expression'. If no case matches
expression's value, control is passed to the default label, if one
exists. Otherwise, control is passed to the statement following the
switch.
Notes: Case labels must be unique.
A case label must must be an integer or character
constant, or a constant expression.
When control is passed to a case label, execution
continues on through the statements following that label.
Be aware that execution does not automatically end at the
next case label, but continues on until either a break,
return or goto statement (or the end of the switch
statement) is encountered. For this reason, you may wish
to put a break after each case's statements--unless, of
course, you want to flow into the next case.
Multiple case labels may prefix the the same statement.
The optional `default:' label need not be the last label
in a switch.
MS-C requires the switch expression be of type char,
short, int or enum. The expression type is widened to
int before being evaluated, as are the case label values.
This allows for 65536 unique cases.
-------------------------------- Example ---------------------------------
while (TRUE) {
switch (getchar()) {
case 'a':
case 'A':
add_record();
break;
case 'd':
case 'D':
delete_record();
break;
...
case EOF:
cleanup();
exit(0);
default:
error("Don't recognize that code. Please try again.");
break;
}
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster