C++ switch statement

In our C++ program, we use the "switch" statement to implement menu-driven code or to test the value of an expression against a list of integer or character constants.

The "switch" statement, which is also known as the "multiple-branch selection statement," is utilized to repeatedly test the value of a specified expression against a list of given integer constants or character constants. In the event that a match is discovered, the statements connected to the respective constant will be carried out.

The syntax of the switch statement is as follows:

switch(expression)
{
   case constant1: 
      // block of code to execute
      // if "constant1" matched the "expression"
      break;
   case constant2: 
      // block of code to execute
      // if "constant2" matched the "expression"
      break;
   case constant3: 
      // block of code to execute
      // if "constant3" matched the "expression"
      break;
   .
   .
   .
   case constantN: 
      // block of code to execute
      // if "constantN" matched the "expression"
      break;
   default:
      // block of code to execute
      // if no match found
      break;
}

After the expression is evaluated, the values that it returns are compared to the values of the constants that are specified in the case statements. When a match is discovered, the block of code (set of statements) associated with that case begins to be carried out. This continues until either the break statement or the end of the switch statement is reached.

If a case statement does not include a break statement, the control will continue on to the next case statement(s) until either a break statement is encountered or the end of the switch is reached. The term "falling through" refers to the circumstance that occurs when there is no break in the case statement. When there is no matching entry, the default statement is the one that is carried out. The default statement is not required, and if it is not present, there will be no action taken if none of the matches are successful.

C++ switch case example program

The following example program illustrates the "switch" case with multiple values. This program prompts the user to enter a week number (1-7) before using the "switch" case to find and print the equivalent name of the day of the week, such as Sunday, Monday, Tuesday,..., Saturday.

#include<iostream>
using namespace std;
int main()
{
   int day;
   cout<<"Enter the day number of the week: ";
   cin>>day;
   switch(day)
   {
      case 1:
         cout<<"Sunday.";
         break;
      case 2:
         cout<<"Monday.";
         break;
      case 3:
         cout<<"Tuesday.";
         break;
      case 4:
         cout<<"Wednesday.";
         break;
      case 5:
         cout<<"Thursday.";
         break;
      case 6:
         cout<<"Friday.";
         break;
      case 7:
         cout<<"Saturday.";
         break;
      default:
         cout<<"Invalid input!";
         break;
   }
   cout<<endl;

   return 0;
}

The following snapshot shows the initial output produced by this C++ program:

c++ switch case example

Now supply the input, say 6, and hit the ENTER key to see the following output:

c++ switch statement program

C++: switch Vs if-else

The switch and if-else statements are both selection statements that allow you to choose an alternative from a set of options by testing an expression.However, there are some differences in their operations. These are given below:

Some important things to know about the C++ switch case

There are some important things that you must know about the switch statement:

Tip: A switch statement is more efficient than a nested if-else statement.

Tip: Always put a break statement after the last case statement in a switch.

One recommended example regarding the "switch" case is the simple calculator program.

C++ Quiz


« Previous Tutorial Next Tutorial »