5 Examples to Learn C++ Switch Case statement

A featured image reflecting C++Switch Case Tutorial

What is C++ Switch Case statement?

The switch case is another decision-making statement that allows you to execute one or a block of statements as a certain condition is true.

  • The switch statement is used with the case statement
  • In the switch, we provide an expression
  • In the case statement, we provide a value
  • Against one switch statement, there can be n case statements
  • The value of the expression in the switch statement is compared with the case value
  • Where a match is found, the block of the statement(s) is executed inside that case statement
  • Generally, each case contains a break statement that terminates the switch/case, and control is moved outside to the next line of code (if available)
  • Normally, a default keyword is also used – its block of code executes when none of the cases match.

Structure of using switch-case statement

Following is the general way of using the C++ switch-case statement;

switch (variable/expression)

{

     case 1:

     //Statement(s) to execute

     break;

     case 2:

     // Statement(s) to execute

     break;

     default:

     // Statement(s) to execute

     ;

}

An example of using switch case statement

In this example, we have an int expression in the switch statement.

Four cases and a default case are used.

See the code and output:

#include<iostream>

using namespace std;

int main()

{

    //Creating an int variable

                int a = 5;

    //Using switch with a as an expression

                switch (a)

                {

                //Cases with different values to be tested against variable a

                case 1:

                                cout<<"The value of variable a = 1";

                                break;

                case 5:

                                cout << "The value of variable a = 5";

                                break;

                case 10:

                                cout << "The value of variable a = 10";

                                break;

                case 15:

                                cout << "The value of variable a = 15";

                                break;

                default:

                                cout<<"Some other value";

                }

                return 0;

}

The output:

CPP-switch

As the value of a=5, the second case is evaluated as true – its code executed, and then the switch statement exited.

An example of switch with user input

  • Almost the same example as above except the user is asked to enter a number.
  • This number is tested against five cases and if evaluated as true, its statement is executed.
  • If the entered value does not match any case, the default case executes and displays its statement.
#include<iostream>

using namespace std;

int main()

{

    //Creating an int variable

                int a;

                cout << "Enter a value of int type: ";

                cin >> a;

    //Using switch with a as an expression

                switch (a)

                {

                //Cases with different values to be tested against variable a

                case 1:

                                cout<<"The value of variable a = 1";

                                break;

                case 5:

                                cout << "The value of variable a = 5";

                                break;

                case 10:

                                cout << "The value of variable a = 10";

                                break;

                case 15:

                                cout << "The value of variable a = 15";

                                break;

                case 20:

                                cout << "The value of variable a = 20";

                                break;

                default:

                                cout<<"Oops, your entered value did not match any case!\n\n";
                }

                return 0;

}

The output:

CPP-switch-input

As we entered the value 50, it did not match any case. So the default case is executed and its message is displayed.

Using char type in the switch statement example

In the following C++ program, we used the char type variable as an expression in the switch case statement.

The user is asked to enter the character for grades – A, B, C, D, or F.

Depending upon the entered value, the related message is displayed:

#include <iostream>

using namespace std;

int main () {

   // local variable declaration:

   char gra = 'D';

               cout << "Enter Your Grade: A,B,C, D or F: ";
               cin >> gra;

   switch(gra) {

      case 'A' :

         cout << "Your Performance is Excellent!" << endl;

         break;

      case 'B' :

         cout << "Your Performance is Good!" << endl;

         break;

      case 'C' :

         cout << "Your Performance is Fine!" << endl;

         break;

      case 'D' :

         cout << "Your Performance is So So!" << endl;

         break;

      case 'F' :

         cout << "Poor Performance!" << endl;

         break;

      default :

         cout << "Enter correct grade!!" << endl;

   }

   return 0;

}

The output:

CPP-switch-char

Display Month Name of the Year Example

In this example, we created twelve cases –one for each month. The user is asked to enter the month number and the C++ program will display the month name as output:

#include<iostream>

using namespace std;

int main()

{

    //Creating an int variable for month

                int mntNum;

                cout << "Enter Month Number: ";

                cin >> mntNum;

    //Using switch with month number expression

                switch (mntNum)

                {

                //12 cases for month names

                case 1:

                                cout<<"The Month is January!";

                                break;

                case 2:

                                cout << "The Month is February!";
                                break;

                case 3:

                                cout << "The Month is March!";
                                break;

                case 4:

                                cout << "The Month is April!";
                                break;

                case 5:

                                cout << "The Month is May!";
                                break;

                case 6:

                               cout << "The Month is June!";
                               break;

                case 7:

                               cout << "The Month is July!";
                               break;

                case 8:

                                cout << "The Month is August!";

                                break;

                case 9:

                                cout << "The Month is September!";
                                break;

                case 10:

                               cout << "The Month is October!";
                               break;

                case 11:

                                cout << "The Month is November!";
                                break;

                case 12:

                                cout << "The Month is December!";
                                break;

                default:

                                cout << "Please enter month number between 1 to 12!";
                                break;

                }

                return 0;

}

The output:

CPP-switch-case

As I entered number 9, the program displayed “September”.

What happens if we do not use the break statement in above program?

Just to show the role of C++ break statement in the switch statement, I have removed the break in each case in the above example.

As In entered number 1 after running the program, see the code and output below:

#include<iostream>

using namespace std;

int main()
{

    //Creating an int variable for month

                int mntNum;
                cout << "Enter Month Number: ";
                cin >> mntNum;

    //Using switch with month number expression

                switch (mntNum)
                {

                //12 cases for month names

                case 1:

                                cout<<"The Month is January!";

                case 2:
                                cout << "The Month is February!";

                case 3:

                                cout << "The Month is March!";

                case 4:
                                cout << "The Month is April!";
                case 5:
                                cout << "The Month is May!";
                case 6:

                                cout << "The Month is June!";

                case 7:
                                cout << "The Month is July!";
                case 8:
                                cout << "The Month is August!";

                case 9:
                                cout << "The Month is September!";
                case 10:
                                cout << "The Month is October!";
                case 11:
                                cout << "The Month is November!";

                case 12:
                                cout << "The Month is December!";

                default:
                                cout << "Please enter month number between 1 to 12!";
                }

                return 0;

}

The output:

CPP-switch-break

You see, all cases are executed and their message is displayed.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!