Site icon Learn 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:

[crayon-6740236f45a43672220193/]

Then, the following snippet:

[crayon-6740236f45a4a287506595/]

could be replaced with:

[crayon-6740236f45a4b186343320/]

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:

[crayon-6740236f45a4d037533913/]

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:

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:

[crayon-6740236f45a4e376041408/]

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:

[crayon-6740236f45a4f203195338/]

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:

[crayon-6740236f45a51539492797/]

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:

[crayon-6740236f45a52325155587/]

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:

[crayon-6740236f45a53356874094/]

Exit mobile version