C++11C++14C++17Learn C++

Switch Statement in Modern C++

The C++ language provides the switch statement which can be used to replace the set of if statements (see If Statements in Modern C++). First of all, let’s define the enum type Traffic_light_color as follows:

Then, the following snippet:

could be replaced with:

The second snippet is easier to read since the intention to compare a value with a set of constants is expressed explicitly. Furthermore, the compilers like the latest generation of Embarcadero RAD Studio C++ can optimize the second snippet above, and instead of brute checking values again and again the compiler can use some implementation of a jump table.

Please note, that each case of the second snippet above ends with return statement since it’s natural way to terminate the case-branch in this example. However, break or goto statements could be also used in order to terminate the case-branches of switch statements. The lack of one or the other way of the case-branch termination would lead to execution of the next case-branch of the switch statement, for example:

In any case, the function println() above will throw the object of type std::logic_error, since the line (4) are executed after the switch statement in the end of call. But depending on the value the different sequences of lines of the code will be executed:

  • if value == Traffic_light_color::red then (1), (2), (3);
  • if Traffic_light_color::yellow then (2), (3);
  • if Traffic_light_color::green then (3).

This situation demonstrates the fall-through the case labels. Fortunately, compilers like Embarcadero RAD Studio C++ can emit a warning if case-branches are not terminated somehow, since unintentional fall-through it’s usually indicates the programmer’s mistake. In order to prevent this behavior (and also the compiler warnings) the most suitable way of case-branch termination here is usage of the return statements to terminate each case-branch:

Of course, there are cases in which a fall-through is intentional, for instance, when the same case-branch is used to handle the range of values:

Please note, that C++17 provides the special attribute [[fallthrough]] which should be used for both to explicitly express that the fall-through is intentional and to prevent a compiler to emit a warning.

Beware, that the default case-branch should never be used in the switch statements where each case-branch is used to handle a value of a enum type. Otherwise, the compiler will never emit the warning about the unspecified case-branches, for example:

In the example above the case to handle Traffic_light_color::yellow value is missed, but the presence of default case-branch signals the compiler that this is intentional and the compiler is forced to keep silence.

As with if statements (see If Statements in Modern C++) it’s possible to introduce variables into the scope of switch statements:

Finally, since C++17 it’s possible to specify the expression that yields a value used for comparing with a set of constants of case-branches just after the variable declared in the scope of switch statement, for example:


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Learn C++Videos

Learn C++ With Design Patterns For Handling/Reporting Errors In C++: Parallel Algorithms And Executors by Mark Hoemmen (CPPCon 2020) Videos

C++Introduction to C++Learn C++

Learn To Use Break And Continue In Loops With C++

C++Introduction to C++Learn C++

Learn To Define And Use Arrays In C++

C++Learn C++Videos

Learn C++ With Back to Basics: Design Pattern by Mike Shah (CPPCon 2020) Video