Tutorial Study Image

Switch case statements in C++


August 17, 2022, Learn eTutorial
1211

In this tutorial let us discuss the switch statements which is supported by C++ for handling decision-making.

The switch statement allows us to choose between several options for executing a block of code.

A switch statement checks a variable for equality against a range of values. Each value is referred to as a case, and each case checks the variable that is being switched on.

The switch statement in C++ has the following syntax


switch (expression)  {
    case constant1:
        // code to be executed if 
        // expression is equal to constant1;
        break;

    case constant2:
        // here the code will be executed only if
        // expression is equal to constant2;
        break;
        .
        .
        .
    default:
        // here the code will be executed only if 
        //here the  expression are not having a match with any constant
}
 

Working of the switch statement in C++

The expression will be evaluated once, then the results are compared with those of each case label.

Suppose, If a match is found, then the code following the matching label will be executed. For example, if the variable's value is constant2, the code following case constant2: will be executed until the break statement is encountered.

If no match is found, the code following default will be executed.

Flow chart for the switch statement

Switch control flowchart

Example for switch Statement in C++

Using a switch statement let us create a calculator


#include <iostream>
using namespace std;

int main() {
    char oper;
    float num1, num2;
    cout << " An operator should be entered (+, -, *, /): ";
    cin >> oper;
    cout << "Please enter two numbers: " << endl;
    cin >> num1 >> num2;

    switch (oper) {
        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;
        case '-':
            cout << num1 << " - " << num2 << " = " << num1 - num2;
            break;
        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;
        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;
        default:
            // operator doesn't match with any case constant (+, -, *, /)
            cout << "Error obtained! The operator is incorrect";
            break;
    }

    return 0;
}

Output:


Output 1
An operator should be entered (+, -, *, /): +
Please enter two numbers: 
20
50
20 + 50 = 70

Output 2
An operator should be entered (+, -, *, /): -
Please enter two numbers: 
9
19
9 - 19 = -10

Output 3
An operator should be entered (+, -, *, /): *
Please enter two numbers: 
7
2
7 * 2 = 14

Output 4
An operator should be entered (+, -, *, /): /
Please enter two numbers: 
10
5
10 / 5 = 2

Output 5
An operator should be entered (+, -, *, /): ?
Please enter two numbers: 
2
3
Error obtained! The operator is incorrect

The switch...case statement is used in the preceding program to perform addition, subtraction, multiplication, and division.

Working on the above program

  1. We first ask the user to type in the desired operator. The char variable named oper is then used to store this input.
  2. The user is then prompted to enter two numbers, which are saved in the float variables  num1 and num2.
  3. The switch statement is then used to verify the operator that the user entered:
    • When the user enters +, the numbers are added together.
    • When the user enters -, the numbers are subtracted.
    • Multiplication is performed on the numbers if the user enters *.
    • When the user enters /, the numbers are divided.
    • The default code is printed if the user enters any other character.
  4. The break statement is used within each case block. This brings the switch statement to an end.
  5. If there is no break statement, all cases following the correct case are executed.