The switch statement

A switch statement is a selection statement that lets you transfer control to different statements within the 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

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.

Read syntax diagramSkip visual syntax diagramswitch statement syntax
 
>>-switch--(--expression--)--switch_body-----------------------><
 

The switch body is enclosed in braces and can contain definitions, declarations, case clauses, and a default clause. Each case clause and default clause can contain statements.

Read syntax diagramSkip visual syntax diagram      .----------------------------------.
      V                                  |
>>-{----+------------------------------+-+---------------------->
        +-type_definition--------------+
        +-file_scope_data_declaration--+
        '-block_scope_data_declaration-'
 
   .-----------------.
   V                 |
>----+-------------+-+--+----------------+---------------------->
     '-case_clause-'    '-default_clause-'
 
   .-----------------.
   V                 |
>----+-------------+-+--}--------------------------------------><
     '-case_clause-'
 
Note:
An initializer within a type_definition, file_scope_data_declaration or block_scope_data_declaration is ignored.

A case clause contains a case label followed by any number of statements. A case clause has the form:

Read syntax diagramSkip visual syntax diagramCase clause syntax
 
               .-----------.
               V           |
>>-case_label----statement-+-----------------------------------><
 

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:

Read syntax diagramSkip visual syntax diagramcase label syntax
 
   .---------------------------------------.
   V                                       |
>>---case--integral_constant_expression--:-+-------------------><
 

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:

Read syntax diagramSkip visual syntax diagramDefault clause statement
 
                                               .-----------.
                                               V           |
>>-+------------+--default--:--+------------+----statement-+---><
   '-case_label-'              '-case_label-'
 

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.



[ Top of Page | Previous Page | Next Page | Contents | Index ]