c++

C++ switch statement

In this article, we are going to learn about switch statement.

The Switch statement basically give us freedom of choice among many alternatives.You can choose from many options.

The syntax of the switch statement in C++ is:

switch (var-name) { 

    case <value1> :
        
        // code to be executed if 
        // expression is equal to value1

        break;
    case <value2> :
        
        // code to be executed if 
        // expression is equal to value2

        break;
    
    default :

        // code to be executed if
        // expression doesn't match any constant

}

How does the switch statement work?

  1. The expression is checked once and looks for the similar value with each label of case.
  2. If value got matched, the corresponding code after the matching label is executed.
  3. If there is no matching found on the above cases then the default code will executed.

Flowchart of switch Statement

Flowchart of C++ switch case statement


Example :

Calculator using switch Statement :-

#include <iostream>
using namespace std;

int main(){
    char calci;
    float dig1, dig2;

    cout << "Kindly enter operator (-, +, /, *): ";
    cin >> calci;

    cout << "Enter two numbers: " << endl;
    cin >> dig1 >> dig2;

    switch (calci){

        case '-':
            cout << dig1 << " - " << dig2 << " = " << dig1 - dig2;
            break;

        case '+':
            cout << num1 << " + " << num2 << " = " << num1 + num2;
            break;

        case '/':
            cout << num1 << " / " << num2 << " = " << num1 / num2;
            break;

        case '*':
            cout << num1 << " * " << num2 << " = " << num1 * num2;
            break;

        default:
            // operator is doesn't match any case constant (-, +, /, *)

            cout << "Error!!!! The operator is wrong ";

    }

    return 0;
}

Output 1 :

Enter an operator (-, +, /, *): +
Enter two numbers:
5.5
1.3
5.5-1.3 = 4.2

Output 2 :

Enter an operator (-, +, /, *): -
Enter two numbers:
5.5
1.3
5.5 + 1.3 = 6.8

Output 3 :

Enter an operator (-, +, /, *): *
Enter two numbers:
5.5
1.3
5.5/1.3 = 4.23

Output 4 :

Enter an operator (-, +, /, *): /
Enter two numbers:
5.5
1.3
5.5 * 1.3 = 7.15

Output 5 :

Enter an operator (-, +, /, *): ?
Enter two numbers:
5.5
1.3
Error!!!! The operator is wrong .

# Stepwise working of Program

  1. We first prompt the user to enter the desired operator. This input is then stored in the char variable named oper.
  2. We then prompt the user to enter two numbers, which are stored in the float variables num1 and num2.
  3. The switch statement is then used to check the operator entered by the user.
  4. If the user enters + , addition is performed on the numbers.
  5. If the user enters - , subtraction is performed on the numbers.
  6. If the user enters * , multiplication is performed on the numbers.
  7. If the user enters / , division is performed on the numbers.
  8. If the user enters any other character, the default code is printed.