The case statement transfers control to one of several places depending on the value of an expression:
labelopt case expression { qual-statement-sequence }
qual-statement-sequence: qual-list => qual-statement-sequence qual-list => qual-statement-sequence statement qual-statement-sequence declaration qual-list: qualifier qual-list or qualifier qualifier: expression expression to expression *
The case statement is executed by comparing the expression at its head with the constants in the qualifiers. The test is for equality in the case of simple constant qualifiers; in range qualifiers, the test determines whether the expression is greater than or equal to the first constant and less than or equal to the second.
None of the ranges or constants may overlap. If no qualifier is selected and there is a * qualifier, then that qualifier is selected.
Once a qualifier is selected, control passes to the set of statements headed by that qualifier. When control reaches the end of that set of statements, control passes to the end of the case statement. If no qualifier is selected, the case statement is skipped.
Each qualifier and the statements following it up to the next qualifier together form a separate separate scope, like a block; declarations within this scope disappear at the next qualifier (or at the end of the statement.)
As an example, this fragment separates small numbers by the initial letter of their spelling:
case i {
1 or 8 =>
sys->print("Begins with a vowel\n)";
0 or 2 to 7 or 9 =>
sys->print("Begins with a consonant\n");
* =>
sys->print("Sorry, didn't understand\n");
}