Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The switch statement

Reference for the C++ switch statement: case labels, fall-through, [[fallthrough]], init statements, scoped variables and enum handling.

switch jumps to the case label whose constant matches the value of an integral or enumeration expression. If no label matches, control goes to default, or past the whole statement if there is no default. It is the tidy alternative to a long if/else if chain that compares one value against constants.

C++ switch falls through: after a matching label runs, execution continues into the next label unless you leave with break, return, throw or goto. This is occasionally useful and frequently a bug, so C++17 added the [[fallthrough]]; attribute to mark intentional cases. There is no pattern matching and no string or floating-point switch.

C++
switch (value) {
    case 1:  /* ... */ break;
    case 2:
    case 3:  /* shared by 2 and 3 */ break;
    default: /* everything else */ break;
}

Try it

C++ Cases, fall-through and default

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Scopes, init statement and [[fallthrough]]

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
switch (condition) statement
switch (init-statement condition) statement   // C++17

case constant-expression : statement
default : statement
Part Description
condition An expression (or declaration) of integral, enumeration or class type that converts to an integral or enumeration type. Floating-point values, pointers and strings are not allowed.
case label A converted constant expression of the promoted condition type. Two labels in one switch must not have the same value.
default Optional; at most one per switch. It may appear anywhere in the body, not only at the end.
statement Usually a braced block that contains the labels.

Usage notes

  • Without break, execution falls through to the next label. Compilers can warn with -Wimplicit-fallthrough (part of -Wextra in GCC). Mark deliberate cases with [[fallthrough]];, which must be directly before the next label.
  • Jumping past a variable initialization into its scope is ill-formed: case 1: int x = 5; break; case 2: fails to compile. Wrap the case body in braces.
  • Switching on an enum class without a default lets GCC and Clang warn (-Wswitch) when you forget an enumerator. Prefer that to a catch-all default, so adding an enumerator produces a warning at every switch.
  • Reading a value that is outside the enum's range is still possible, so put a return, throw or std::unreachable() (C++23) after the switch when you rely on it being exhaustive.
  • The condition is promoted before comparison, so char and short conditions work with integer case values.
  • A case label can appear inside nested statements (Duff's device), but that is rarely a good idea.
  • break inside a switch leaves the switch, not an enclosing loop. To leave a loop from inside a switch use return, a flag or goto.
  • Compilers may implement dense switches as jump tables and sparse ones as binary searches. Do not assume it is faster than if chains; measure.
  • Case ranges (case 1 ... 5) are a GCC extension, not standard C++.