Code has been added to clipboard!

How to Use the Switch Statement in C++

Reading time 3 min
Published Sep 3, 2019
Updated Sep 6, 2019

TL;DR Switch statement in C++ allows creating multiple answers and results. It's an alternative to the if statement.

What is a C++ Switch Statement?

The switch statement works as a multiway branch statement, meaning that you can create multiple answers and results with short commands. It’s a common alternative to the if statement when you want to get multiple results.

Since we’ll be creating multiple cases using the switch statement, we need to understand the flow of the switch statement’s system. It’s recommended to use a flowchart to create a better-structured switch statement. Here’s an example:

Switch Statement C++

Switch Statement Syntax

Let’s breakdown the switch statement’s syntax:

Example
#include <iostream>

using namespace std;

int main() {
    int k = 1;
    
    switch (k) {
    case 1: // will be executed if k = 1;
        break;
    case 2: // will be executed if k = 2;
        break;
    default: // will be executed if k doesn't match any cases
        break;
    }
}

Here k is the name of the result we want to get. Meanwhile, the cases are the results that come out if k is answered with the specified value.

For example, you want to have output as a result, if the answer is 1. The result needs to be written after case 1. You also need to make sure that the value of k in case 1 is 1. So, if anybody answers the variable with 1, the result output will appear.

After creating your case, it is essential to follow up the case with a break command. Break instructs the program to stop looking for the answer if one is already found.

Once you are finished with case 1, you can continue with case 2, case 3, and so on. The beauty of the switch statement is that you can put as many cases as needed.

After creating all cases, you have to close the statement with the default result. It will be outputted if the answer is not in any of the cases.

Most Common Switch Statement Application

Let’s look at an example of the switch statement in action:

Example
#include <iostream>

using namespace std;

int main() {
    char choice;

    switch (choice) {
    case 'N':
        cout << "No";
        break;
    case 'D':
        cout << "Don't know";
        break;
    case 'Y':
        cout << "Yes";
        break;
    default:
        cout << "N/A";
    }
}

In the example above, we see that the result of case Y is Yes while the result of case N is No. If the inputted value matches none of the three defined options, the produced result will be Invalid response.

Switch Statement in C++: Useful Tips

  • You can use non-numerical values with the switch statement.
  • The cases do not have to be in order, as long as you use proper requirements and syntax.
  • The cases cannot have the same value, as this will result in a duplicate value error.
  • You don’t have to use a break statement for each case, but the action will keep happening until it reaches another break statement.