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:

  • The switch statement differs from the if statement in that the former can only test for equality, whereas the latter can evaluate a relational or logical expression, i.e., multiple conditions.
  • The switch statement selects its branches by testing the value of the same variable (against a set of constants), whereas the if-else construction lets you use a series of expressions that may involve unrelated variables and complex expressions.
  • The if-else statement is the more versatile of the two.For example, if-else can handle ranges, whereas switch cannot. Each switch case label must be a single value.
  • The if-else statement can handle floating-point tests as well as integer and character tests, whereas a switch cannot handle floating-point tests. The case labels of switches must be integers (including chars).
  • The switch case label value must be a constant. So, if two or more variables are to be compared, use if-else.
  • In terms of code, the switch statement is a more efficient choice in situations where the nature of switch operation (testing a value against a set of constants) is supported.

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

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

  • A switch statement can only work for equality comparisons.
  • No two case labels on the same switch can have identical values. But, in the case of nested switch statements, the case constants of the inner and outer switch can have common values.
  • If character constants are used in the switch statement, they are automatically converted to their integer equivalents (i.e., their equivalent ASCII codes).
  • The switch statement is more efficient than if it were used in a situation that supported the nature of switch operation.

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 »


Follow/Like Us on Facebook


Subscribe Us on YouTube