Name
switch statement — Multibranch selection statement
Synopsis
statement := switch ( condition ) statement
condition ::= expression | type-specifier-seq
declarator = assignment-expr
The switch statement
evaluates condition and saves the
value. It compares the value to each case label in statement (which is typically a compound
statement). The statements that follow the matching case label are executed. If no case label matches, the statements following
the default label are executed. If
there is no default, execution
continues with the statement following the end of the switch statement.
All case labels in statement must be unique. Use the break statement to exit the switch statement at the end of each
case.
Example
switch(c) {
case '+':
z = add(x, y);
break;
default:
z = noop( );
break;
};See Also
break, case, declarator, default, expression, if, statement, type, Chapter
4
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access