Basics of Switch Statements in C++ and How To Use Them


The switch statement in C++ can be very useful in a number of situations. Often times it is used in place of if-else statements to improve readability and efficiency of code. Learning to use the switch statement is fundamental to learning C++ computer programming. It provides a clear and concise way to check a variable against a range of values and execute different statements based on the set value. In this tutorial lets get a detailed look at the basics of switch statements and how to use them. The goal at the end of it is to have a general understand of why you might want to use a switch statement and what advantage it gives you over statements of similar types.

Switch Statement Practical Explanation


A simple and effective way to start thinking about using switch statements it to think about actual physical switches. When you flip a switch it will do whatever that switch was designed to do, whether that by to turn on a light or change a setting for a device. Take a light switch for example. Suppose you have 4 light switches that turn on different lights when you flip them. If you flip the first switch it turns on the first light and the third switch turns on the third light and so on. In C++ programming, switches are used in a similar way. When you activate a switch it does something. In particular, it executes the statement associated with it. 

How Does The C++ Switch Statement Work?


C++ switch statement flowchart

The switch statement compares the value of a variable to a value called a “case”. If the variable’s value is equal to the specified case then the program will execute the statement for that case. Let’s take a look at the flowchart on the right. Initially, the program will start and a variable must be set. The variable must be set to a value before the program begins checking the “case” values. This can be done in a number of ways including user input, hard-coding, etc. Once a value has been set for the variable used in the switch statement, or even if one hasn’t at this point, the program will match the variable up with the appropriate case. If it matches no case values then none of the case statements will run and the default statement will run instead. The default statement functions exactly as it sounds. If no case values match up with your variable used in your switch statement the default statement will run instead. Note that the switch statement shown in the flowchart only has 3 possible case values but indeed a switch statement can have any number of case values.

Example Codes


C++ switch statements take the form of the following:

switch(variable)
{
    case 1:
        statement;
    case 2:
        statement;
    case 3:
        statement;
    default:
        default statement;
}

As you can see the switch statement must have a conditional variable. If that variable has the same value as that with a case then it will run the statement within that case. If the variable matches up with no values within any of the cases then the default statement will run. Suppose you wanted to create a switch statement where you can input an integer value “n” between 1 and 3. If you input n then the console should output “You have selected case n” then exit the switch. In the following code example we have just that. The program will start then it will ask you to input a value for the variable. The program will then check the variable against the case values and run the appropriate code within the switch. The code output with an input of 1 is shown below.

#include <iostream>
int main()
{
int variable;
std::cout << "Please enter a value between 1 and 3.n";
std::cin >> variable;
switch(variable)
    {
    case 1:
        std::cout << "You have selected case 1";
        break;
    case 2:
        std::cout << "You have selected case 2";
        break;
    case 3:
        std::cout << "You have selected case 3";
        break;
    default:
        std::cout << "That isn't a valid choice";
    }
return 0;
}

C++ switch statement code output image

Another example of a switch statement is shown below in which, instead of an integer value, you want to input a char value instead. In this case the inputs can be either A, B, or C. Note that inputs in this way are case sensitive. Any input that isn’t in the proper case will result in the default statement being executed.

#include <iostream>
char variable;
int main(){
    std::cout << "Which option would you like to select?" << std::endl;
    std::cout << "Option AnOption BnOption Cn";
    std::cin >> variable;
    switch(variable)
    {
        case 'A':
            std::cout << "You have selected option An";
            break;
        case 'B':
            std::cout << "You have selected option Bn";
            break;
        case 'C':
            std::cout << "You have selected option Cn";
            break;
        default:
            std::cout << "That isn't a valid responsen";
    }
return 0;
}

C++ switch with char output