Skip to Main Content
C++ In a Nutshell
book

C++ In a Nutshell

by Ray Lischner
May 2003
Intermediate to advanced content levelIntermediate to advanced
808 pages
32h 24m
English
O'Reilly Media, Inc.
Content preview from C++ In a Nutshell

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.
Start your free trial

You might also like

C++ High Performance

C++ High Performance

Viktor Sehr, Björn Andrist
Optimized C++

Optimized C++

Kurt Guntheroth
Mastering C++ Programming

Mastering C++ Programming

Jeganathan Swaminathan

Publisher Resources

ISBN: 059600298XSupplemental ContentErrata Page