The control structure switch case

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Control structures

    /

  5. The control structure switch case

The switch case structure

The switch case structure is used to execute a different section of code depending on the value of a variable. Unlike the structure if which works with conditions, the structure switch case only works with values. That structure searches for a section of code, inside its braces {}, assigned with the same value as the value of the variable inside its parentheses (). If it finds one, those instructions are executed until either a break instruction or the end of the structure is reached.

The switch case structure is coded by writing the keyword switch followed by the variable to test inside parentheses (). Below that, inside braces {}, for each value we want to test, we write the keyword case followed by the value and a colon :, then the instructions, ended with a break instruction, to execute if the value of the variable is the one after the case keyword.

Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> int main() { int var = 1; switch(var) { case 0: std::cout << "var equals 0." << std::endl; break; case 1: std::cout << "var equals 1." << std::endl; break; case 2: std::cout << "var equals 2." << std::endl; break; } return 0; }

Note that the value following a case keyword must be a value and can not be a variable.

The default statement

The keyword default can be used to specify the code to execute if the variable tested equals none of the values following the case keywords inside the switch case structure. The default statement is written by writing the keyword default followed by a colon : and then instructions to execute if it is reached.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream> int main() { std::cout << "Enter your age: "; unsigned age; std::cin >> age; age /= 10; // age is assigned with the amount of full 10 years inside the age. switch(age) { case 0: // Younger than 10 std::cout << "You are younger than 10 years old." << std::endl; break; case 1: // Younger than 20 std::cout << "You are younger than 20 years old, older than 9." << std::endl; break; case 2: // Younger than 30 std::cout << "You are younger than 30 years old, older than 19." << std::endl; break; default: // Older than 29 std::cout << "You are older than 29 years old." << std::endl; } return 0; }

If we execute the example above and enter 15 to the console, the instructions inside the case where the value is 1 will be executed. The first instruction would print "You are younger than 20 years old, older than 9." to the console and the second instruction (break;) would cause the program to leave the switch case structure (To continue its execution at the end of it).

If we entered 40, the code after the default statement would be executed and the text "You are older than 29 years old." would be printed in the console. The program would then continue its execution after the switch statement even though there is no break instruction, because there is no other case statement after it. The break instruction is not required for the default statement or last case statement if there is no default statement.

The break instruction

If we did not write any break instruction after the case statements, like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream> int main() { std::cout << "Enter your age:"; unsigned age; std::cin >> age; age /= 10; // age is assigned with the amount of full 10 years inside the age. switch(age) { case 0: // Younger than 10 std::cout << "You are younger than 10 years old." << std::endl; case 1: // Younger than 20 std::cout << "You are younger than 20 years old, older than 9." << std::endl; case 2: // Younger than 30 std::cout << "You are younger than 30 years old, older than 19." << std::endl; default: // Older than 29 std::cout << "You are older than 29 years old." << std::endl; } return 0; }

Then, when the processor would execute the instructions of a case statement, it would continue its execution even after the end of the code following the case statement and execute the code in the case statements below it, because there would not be any break instruction to make it leave the switch case structure.

Switch case vs if

Any switch case structure can be translated into an if structure, but not all if structures can be translated into switch case structures. A switch case structure may, sometimes, when there are many possibilities to test, be faster than an if statement (Otherwise it is as fast). For that reason, it is recommended to use switch case structures over if structures when it is possible and there are many values to test.

The reason switch case structures can be faster than if structures is that, while an if structure will test all the conditions until one is true or the end of the structure is reached (10 tests if there are 9 else if and 1 if statements and all the conditions inside it are false), switch case structures can sometime be optimized by the compiler so the processor directly jumps to the code assigned with the case that has the same value as the variable, instead of testing all the values.