🔥 $100K Hit! Where Will Bitcoin Go Next? Find Out Live!

Code has been added to clipboard!

How to Use the Switch Statement in C++

Reading time 3 min
Published Sep 3, 2019
Updated Sep 27, 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.

DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

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.