Transfers control to one of several statements, depending on the value of a condition.
Note that any init-statement must end with a semicolon. This is why it is often described informally as an expression or a declaration followed by a semicolon.
condition - a condition statement - a statement (typically a compound statement)A condition can either be an expression or a simple declaration.
When control reaches condition, the condition will yield a value, which is used to determine which label the control will go to.
If condition is an expression, the value it yields is the the value of the expression.
If condition is a simple declaration, the value it yields is the value of the decision variable (see below).
The declaration has the following restrictions:
The decision variable of the declaration is the declared variable.
The declaration has the following restrictions:
The decision variable of the declaration is the invented variable e introduced by the declaration.
(since C++26)condition can only yield the following types:
If the yielded value is of a class type, it is contextually implicitly converted to an integral or enumeration type.
If the (possibly converted) type is subject to integral promotions , the yielded value is converted to the promoted type.
Any statement within the switch statement can be labeled with one or more following labels:
attr (optional) case constant-expression : (1) attr (optional) default: (2) attr - (since C++11) any number of attributes constant-expression - a converted constant expression of the adjusted type of the switch conditionA case or default label is associated with the innermost switch statement enclosing it.
If any of the following conditions is satisfied, the program is ill-formed:
When the condition of a switch statement yields a (possibly converted) value:
case and default labels in themselves do not alter the flow of control. To exit from a switch statement from the middle, see break statements.
Compilers may issue warnings on fallthrough (reaching the next case or default label without a break) unless the attribute [[fallthrough]] appears immediately before the case label to indicate that the fallthrough is intentional(since C++17).
If init-statement is used, the switch statement is equivalent to
{ init-statement switch ( condition ) statement}
Except that names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope, which is also the scope of statement.
(since C++17)Because transfer of control is not permitted to enter the scope of a variable, if a declaration statement is encountered inside the statement, it has to be scoped in its own compound statement:
switch, case, default
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior CWG 1767 C++98 condition s of types that are not subject tointegral promotion could not be promoted do not promotecondition s of these types CWG 2629 C++98 condition could be a declaration of a floating-point variable prohibited
Link nội dung: https://www.sachhayonline.com/switch-case-c-a42609.html