| Labels | ||||
| Expression statements | ||||
| Compound statements | ||||
| Selection statements | ||||
switch | ||||
| Iteration statements | ||||
| Jump statements | ||||
Executes code according to the value of an integral argument.
Used where one or several out of many branches of code need to be executed according to an integral value.
Contents |
attr-spec-seq(optional)switch (expression)statement | |||||||||
| attr-spec-seq | - | (C23)optional list ofattributes, applied to theswitch statement |
| expression | - | anyexpression ofinteger type (char, signed or unsigned integer, or enumeration) |
| statement | - | anystatement (typically a compound statement).case: anddefault: labels are permitted instatement, andbreak; statement has special meaning. |
caseconstant-expression:statement | (1) | (until C23) | |||||||
attr-spec-seq(optional)caseconstant-expression:statement(optional) | (1) | (since C23) | |||||||
default:statement | (2) | (until C23) | |||||||
attr-spec-seq(optional)default:statement(optional) | (2) | (since C23) | |||||||
| constant-expression | - | any integerconstant expression |
| attr-spec-seq | - | (C23)optional list ofattributes, applied to the label |
The body of a switch statement may have an arbitrary number ofcase: labels, as long as the values of allconstant-expressions are unique (afterconversion to thepromoted type ofexpression). At most onedefault: label may be present (although nested switch statements may use their owndefault: labels or havecase: labels whose constants are identical to the ones used in the enclosing switch).
Ifexpression evaluates to the value that is equal to the value of one ofconstant-expressions after conversion to the promoted type ofexpression, then control is transferred to the statement that is labeled with thatconstant-expression.
Ifexpression evaluates to a value that doesn't match any of thecase: labels, and thedefault: label is present, control is transferred to the statement labeled with thedefault: label.
Ifexpression evaluates to a value that doesn't match any of thecase: labels, and thedefault: label is not present, none of the switch body is executed.
Thebreak statement, when encountered anywhere instatement, exits the switch statement:
As with all other selection and iteration statements, the switch statement establishesblock scope: any identifier introduced in theexpression goes out of scope after thestatement. If a VLA or another identifier with variably-modified type has a switch(expr){int i=4;// not a VLA; OK to declare here f(i);// never called// int a[i]; // error: VLA cannot be declared herecase0: i=17;default:int a[i];// OK to declare VLA hereprintf("%d\n", i);// prints 17 if expr == 0, prints indeterminate value otherwise} | (since C99) |
Output:
func(1): case 1, case 2, case 3.func(2): case 2, case 3.func(3): case 3.func(4): case 4, case 5 or case 6, default.func(5): case 5 or case 6, default.func(6): case 5 or case 6, default.func(7): default.func(8): default.
C++ documentation for switch statement |