Switch case statement in C++

Switch statement is also used to control the flow of program and most of the cases it can be replaced by if else statement. Similarly, many if else statement can be replaced by the switch statement.

Let’s see the syntax of switch statement first.

// syntax of switch statement

switch(expression){
    case a:
        // this code will be executed when the expression is a
        break;
        
    case b:
        // this code will be executed when the expression is b
        break;
        
    case c:
        // this code will be executed when the expression is c
        break;
        
    .........................
    .........................
    
    default:
        // default code to execute when
        // the expression does not match with any case
}

When the expression which we have passed through the switch statement will match with the first case, then the code bellow it will be executed. We have used break statement because if we don’t use a break statement the other case bellow the matched case will also be executed which is not expected. We will learn more about break statement later.

The code under default case will be executed when the expression does not match with any case.

Let’s see an executable program using C++ switch statement. In this bellow program we will make a simple calculator using switch statement.

C++ program using switch statement

// making a calculator using switch statement in C++

#include <iostream>
using namespace std;

int main(){
    int selectedOperation;
    float firstNum, secondNum;

    cout << "Select which operation you want to do.\n";
    cout << "1. Additon\n";
    cout << "2. Subtraction\n";
    cout << "3. Multiplication\n";
    cout << "4. Division\n";
    cout << "\nSelect operation : ";

    cin >> selectedOperation;

    cout << "Enter first number here : ";
    cin >> firstNum;
    cout << "Enter second number here : ";
    cin >> secondNum;

    switch (selectedOperation){
        case 1:
            cout << firstNum << " + " << secondNum << " = " << firstNum + secondNum << endl;
            break;

        case 2:
            cout << firstNum << " - " << secondNum << " = " << firstNum - secondNum << endl;
            break;

        case 3:
            cout << firstNum << " * " << secondNum << " = " << firstNum * secondNum << endl;
            break;

        case 4:
            cout << firstNum << " / " << secondNum << " = " << firstNum / secondNum << endl;
            break;

        default:
            cout << "Error! operator not applicable\n";
            break;
    }

    return 0;
}

In this above program we will take the input from user that which operation he want to do.

After that we will take two number and perform that operation. Let’s see the output of above program.

Output of switch case program:

Assume a user has selected first operation. Then the output will be as bellow;

switch-statement-in-c-c-switch-case-example

Similarly, if the user select the third operation then the output will be as follows;

switch-case-in-c-switch-statement

The default keyword will work if the user gives other number without 1,  2,  3 or 4.

Select which operation you want to do.
1. Additon
2. Subtraction
3. Multiplication
4. Division

Select operation : 5
Enter first number here : 34
Enter second number here : 21
Error! operator not applicable

Remember when working with switch statement

  • We can use any valid expression as the case of switch statement.
float a;

switch(a){      // error! switch quantity no an integer
    case 2.5:
}
  • Using float or double number as expression will through an error.
int x;

switch(x){
    case 3 + 5:      // valid. 3 + 5 = 8
}
  • Using character variable as expression is valid in C++ switch statement.
char c;

switch(c){
case 'p':      // valid
    
    // executable code
    
case p:        // invalid. character should be inside the single quote
    
    // executable code
}

Previous pagenested if else in C++

Next pagefor loop in C++