The switch statement
switch body
depending on the value of the switch expression. The switch expression
must evaluate to an integral or enumeration value. The body of the switch statement
contains case clauses that consist of - A
caselabel - An optional
defaultlabel - A
caseexpression - A list of statements.
If the value of the switch expression equals the
value of one of the case expressions, the statements following that
case expression are processed. If not, the default label statements,
if any, are processed.
A case clause contains a case label followed by any number of statements. A case clause has the form:
A case
label contains the word case followed by an integral
constant expression and a colon. The value of each integral constant
expression must represent a different value; you cannot have duplicate case labels.
Anywhere you can put one case label, you can put
multiple case labels. A case label has the form:
A default clause contains a default label
followed by one or more statements. You can put a case label
on either side of the default label. A switch statement
can have only one default label. A default_clause has
the form:
The switch statement passes control to the statement
following one of the labels or to the statement following the switch body.
The value of the expression that precedes the switch body
determines which statement receives control. This expression is called
the switch expression.
The value of the switch expression is compared
with the value of the expression in each case label.
If a matching value is found, control is passed to the statement following
the case label that contains the matching value.
If there is no matching value but there is a default label
in the switch body, control passes to the default labelled
statement. If no matching value is found, and there is no default label
anywhere in the switch body, no part of the switch body
is processed.
When control passes to a statement in the switch body,
control only leaves the switch body when a break statement
is encountered or the last statement in the switch body
is processed.
If necessary, an integral promotion is performed on the controlling
expression, and all expressions in the case statements
are converted to the same type as the controlling expression. The switch expression
can also be of class type if there is a single conversion to integral
or enumeration type.
Compiling with option CHECKOUT(*GENERAL) finds case labels that fall through when they should not.
