switch…case statement in C++ Programming

The switch…case statement is a multiple branching statement where the control is transferred to one of the many possible conditions by checking the value of a variable or an expression. The switch statement consists of different cases inside it and the statements inside the case matching the condition is executed. If no cases are matched, the statements inside default block are executed.

Switch case are similar to if statements. But when a large number of conditions are to be checked, switch case are more suitable than if statements. It reduces programmers burden of using multiple else if statements. The switch case make program more easy to understand in such case.

Syntax of switch…case

switch (variable or expression)
{
    case value1:
        statement(s);
        break;
    case value2:
        statement(s);
        break;
    ... ... ...
    ... ... ...
    case valueN:
        statement(s);
        break;
    default:
        statement(s);
        break;
}

Flowchart for switch…case

Flowchart of switch case in C++ Programming

break Statement

The break statement is used to break out of a loop or a switch case. In switch..case, if a case is matched then, all the cases below it are executed. So, break statement is used after each case in order to break out of switch..case after a case has been matched.

Syntax of break Statement

break;

C++ examples for switch case statement

Example 1: C++ program to determine if a letter is vowel or consonant (not using break)

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char c;
    cout <<"Enter a alphabet:";
    cin >> c;
    c=tolower(c);
    switch (c)
    {
        case 'a':
            cout <<"You entered a, its a vowel"<<endl;
        case 'e':
            cout <<"You entered e, its a vowel"<<endl;
        case 'i':
            cout <<"You entered i, its a vowel"<<endl;
        case 'o':
            cout <<"You entered o, its a vowel"<<endl;
        case 'u':
            cout <<"You entered u, its a vowel"<<endl;
        default:
            cout <<"You entered consonant";
    }
    getch();
    return 0;
}

Output

Enter a alphabet:i
You entered i, its a vowel
You entered o, its a vowel
You entered u, its a vowel
You entered consonant

Example 2: C++ program to determine if a letter is vowel or consonant (using break)

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    char c;
    cout <<"Enter a alphabet:";
    cin >> c;
    c=tolower(c);
    switch (c)
    {
        case 'a':
            cout <<"You entered a, its a vowel"<<endl;
            break;
        case 'e':
            cout <<"You entered e, its a vowel"<<endl;
            break;
        case 'i':
            cout <<"You entered i, its a vowel"<<endl;
            break;
        case 'o':
            cout <<"You entered o, its a vowel"<<endl;
            break;
        case 'u':
            cout <<"You entered u, its a vowel"<<endl;
            break;
        default:
            cout <<"You entered consonant";
    }
    getch();
    return 0;
}

Output

Enter a alphabet:i
You entered i, its a vowel

The above two examples explain the importance of break statement in switch case. Both programs do the same thing i.e. asks an alphabet from user and if it is vowel, identifies which vowel it is else display it is consonant. But the only difference is, there is use of break statement is second example. So, only the statements inside matching case are executed by second example while the first example executes all the statements below the matching case.

Example 3: C++ program to check percentage of a student and display division(distinction, first, second, third or fail)

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    float percent;
    int x;
    cout<<"Enter your percentage: ";
    cin>>percent;
    cout<<"You scored "<<percent<<"%"<<endl;
    x = percent/10;
    switch (x)
    {
        case 10:
        case 9:
        case 8:
            cout<<"You have passed with distinction";
            break;
        case 7:
        case 6: 
            cout<<"You have passed with first division";
            break;
        case 5:
            cout<<"You have passed with second division";
            break;
        case 4:
            cout<<"You have passed with third division";
            break;
        default:
            cout<<"Sorry: You have failed";
    }
    getch();
    return 0;
}

This program asks the percentage of a student and display which division he/she has got. The criteria for division is displayed below:

Percentage Division
>=80 Distinction
>=60 and <80 First division
>=50 and <60 Second Division
>=40 and <50 Third Division
<40 Fail

According to the condition, a switch case is created and the results are displayed.

Output

Enter your percentage: 89
You scored 89%
You have passed with distinction
Enter your percentage: 47
You scored 47%
You have passed with third division