Switch case statement in C++

In this tutorial, we are going to explore the switch case statement in C++. A switch case statement is often used in our C++ programs when we are having many cases(blocks), and we need to execute some particular block, which matches the given expression.

For example, if you were to create a calculator, you would take operands, and an operator, for determining which operation needs to be done. So, we have multiple operators, so we write multiple cases, and according to the operator entered, the matching case will be executed.

Switch case statement in CPP

Do not worry if you do not get the broad picture, since we are going to have a look at the syntax of switch case in C++, and we will also go through an example, to understand the use of switch case in our C++ programs.

Here is the syntax for the switch case statement –

#include <iostream>

int main()
{

    switch(expression)
    {
      case expr1 : // some code here.
        break;
      case expr2 : // some code here.
        break;
      case expr3 : // some code here.
        break;
      case expr4 : // some code here.  
        break;
      default: // some code here.
        break;
    }
return 0;

}

As you can see in the syntax, we have some of the usual things from the syntax, and then we have the switch keyword, after which, we are going to have some expression, and within the block, we have different cases, where we are going to write some code for some particular case.

For example, let’s say that we are creating a calculator, and we are getting the operator and 2 operands as user input. In this case, we are going to do the operation on the basis of what operator is there. So, for all the arithmetic operators, we will have the cases, and we will watch for what operator matches, and we will execute that case.

Notice the break keyword at the end of every case? Well, this is just to get out after executing that particular case. In case if break keyword is not there, it will move to the next case executing it, even if that case doesn’t qualify. It would keep doing this, till the cases are finished, or it finds a break keyword.

Also, we have the default at the last(it is not compulsory to keep it in the last). This default executes if there was no matching case for a given expression.

So, now, let’s turn to an example, to understand the switch case statement in a better way, and to see it in action.

#include <iostream>

int main()
{
    int var1, var2;
    char operation;

    std::cout << “Please enter the Operator : “;
    std::cin >> operation;

    std::cout << “Please enter first number : “;
    std::cin >> var1;

    std::cout << “Please enter second number : “;
    std::cin >> var2;

    switch(operation)
    {
      case ‘+’ : std::cout << “Performing addition:”;
        std::cout << var1 + var2 << std::endl;
        break;
      case ‘-‘ : std::cout << “Performing subtraction:”;
        std::cout << var1 – var2 << std::endl;
        break;
      case ‘*’ : std::cout << “Performing multiplication:”;
        std::cout << var1 * var2 << std::endl;
        break;
      case ‘/’ : std::cout << “Performing division:”;
        std::cout << var1 / var2 << std::endl;
        break;
      default: std::cout << “This input was not expected…”;
        break;
    }
return 0;
}

As you can see from the above program, we are taking the operator, and two operands as user input, and then we are having the switch block. Here, we are trying to match the operator, and accordingly, we will execute the matching case for the operation.
Here is the output –

Please enter the Operator : *
Please enter first number: 23
Please enter the second number: 5
Performing multiplication:115

As you can see, we had given the operator symbol for multiplication, and it performed multiplication as required. So, you can use the concept of switch case statements in a number of use cases, similar to this. It is usually used in situations when we are having some cases, and we need to execute some particular case that matches the given expression.

Remember that if no case matches, the default will be executed, and do not forget to write the break keyword there, since it would help you to get out of the switch block, after executing the relevant case. Missing break keywords would result in a fall-through.

FAQs related to switching case statements in C++

Q: Is switching a keyword in C++?

Ans: Yes, the switch is a keyword in C++.

Q: why there is a break in the switch case statement?

Ans: the break keyword helps get out of the switch block, after executing the relevant case. If it is not present, it will result in a fall-through. It simply means that it would execute the cases after that as well, even if they are not relevant.

Q: What is the default in the switch case?

Ans: If no case matches, then the default case will execute.