C++ 16: Switch Statement

The switch statement is used to test an int or char variable to a list of values and execute an appropriate block of code (case block) as per the value of the variable.

Syntax:

    switch(variable){

        case_val_1:

                            ........

                            break;

        case_val_2:

                            ..........

                            break;

        case_val_n:

                            ..........

                            break;

        default :

                    .......

    }

Program:

#include <iostream>
using namespace std;
main()
{
    int i;

    cout << "1:Hindi" << endl;
    cout << "2:English" << endl;
    cout << "3:Marathi" << endl;
    cout << "Enter valid Input :";
    cin >> i;

    switch (i)
    {
    case 1:
        cout << "You Selcted Hindi" << endl;
        break;
    case 2:
        cout << "You Selcted English" << endl;
        break;
    case 3:
        cout << "You Selcted Marathi" << endl;
        break;
    default:
        cout << "Enter valid Input" << endl;
    }
}

Output:

1:Hindi
2:English
3:Marathi
Enter valid Input :2
You Selcted English













Comments