switch

A switch statement 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 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.

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.

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 label contains the word case followed by an integral constant expression and a colon. Anywhere you can put one case label, you can put multiple case labels.

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.

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.

Restrictions
The switch expression and the case expressions must have an integral type. The value of each case expression must represent a different value and must be a constant expression.

Only one default label can occur in each switch statement. You cannot have duplicate case labels in a switch statement.

You can put data definitions at the beginning of the switch body, but the compiler does not initialize auto and register variables at the beginning of a switch body.

You can have declarations in the body of the switch statement. In C++, you cannot transfer control over a declaration containing an initializer unless the declaration is located in an inner block that is completely bypassed by the transfer of control. All declarations within the body of a switch statement that contain initializers must be contained in an inner block.



break Statement