Information Technology Reference
In-Depth Information
The switch Statement
The switch statement implements multi-way branching. The syntax and structure of the
switch statement are shown in Figure 9-3.
￿The switch statement contains zero or more switch sections .
￿Each switch section starts with one or more switch labels .
Figure 9-3. Structure of a switch statement
Switch labels have the following form:
case ConstantExpression :
Keyword Switch label terminator
The flow of control through the structure in Figure 9-3 is the following:
Test expression TestExpr is evaluated at the top of the construct.
￿
If the value of TestExpr is equal to the value ConstExpr1 , the constant expression in the
first switch label, then the statements in the statement list following the switch label are
executed, until the break statement is encountered.
￿
Each switch section must end with a break statement (or a goto statement, as discussed
later).
￿
￿The break statement branches execution to the end of the switch statement.
￿The default case is optional.
Search WWH ::




Custom Search