Switch statement in C++

- February 16, 2018
In C++ programming, a switch statement is a type of selection control mechanism used to select one choice when multiple choices are given. It can be used as an alternative of if else if structure.
Switch statements function somewhat similar to the if statement usually used for option menu selections. The switch structure contains only one expression at its beginning and multiple cases within its body. Each case consists of a statement or set of statements and end with break statement.
In C++, the general syntax of switch statement:
switch (expression)
   {
      case label-1:
            set of statements-1;
            break;
      case label-2:
            set of statements-2;
            break;
      case label-3:
            set of statements-3;
            break;
      ----    ---------------
      ----    ---------------
      default:
            set of statements-n;
   }
In the above syntax:
switch It is a keyword that indicates the beginning of "switch" structure.
expression It is an expression that may be a single variable (or an arithmetic expression). Its returned value should be an integer or a single character.
case It is a keyword that is used in switch statement to match the value according to the given expression.
label-1 ..... label-3 They represents the labels for the each case. Every label must be unique, numeric constant or character constant value. The value returned by expression must be same as the data type of case labels
default It is a keyword that is used in switch structure to execute the statement ("set of statements-n;") as default if the value returned by the expression doesn't match with any case label.

The 'default' keyword can be placed at any position in the switch structure. For example, it can be placed at the beginning of switch structure. Its use is optional.
The value returned by the expression must not be a double or float value (or any other types of data).

Flowchart of switch statement

The flowchart of c++ switch structure is as follows:
 Cpp switch statement execution

Switch Statement Example Programs

Example 1: The following source code of the program inputs an integer value and displays the corresponding name of day of the week using switch selection structure.
#include<iostream>
using namespace std;
main()
 {
   int num;
   cout<<"Enter an integer value (from 1 to 7):";
   cin>>num;
   switch(num)
   {
      case 1:
            cout<<"Monday";
            break;
      case 2:
            cout<<"Tuesdays";
            break;
      case 3:
            cout<<"Wednesday";
            break;
      case 4:
            cout<<"Thursday";
            break;
      case 5:
            cout<<"Friday";
            break;
      case 6:
            cout<<"Saturday";
            break;
     case 7:
            cout<<"Sunday";
            break;
      default:
            cout<<"Please enter valid number of the day";
    }
return 0;
 }
Output of the program:
Enter an integer value (from 1 to 7): 6
Saturday

Example 2: The following source code of the program inputs a single character and check whether it is a vowel or constant by using switch statement.
#include<iostream>
using namespace std;
main()
 {
   char ch;
   clrscr();
   cout<<"Enter a single character:";
   cin>>ch;
   switch (ch)
   {
       case 'a' :
       case 'A' :
       case 'e' :
       case 'E' :
       case 'i' :
       case 'I' :
       case 'o' :
       case 'O' :
       case 'u' :
       case 'U' :
            cout<<"It is a vowel";
            break;
      default:
            cout<<"It is a constant";
   }
return 0;
 } //end main function
Output of the program:
Enter a single character:  A
It is a vowel

Simple calculator program in c++ using switch case

#include<iostream>
using namespace std;
main()
  {
   float n, m;
   char opt;
   clrscr();
   cout<<"Enter first value, arithmetic operator (+, -, * or /) and second value: \n";
   cin>n>>opt>>m;
   switch (opt)
   {
      case '+' : cout<<n+m; break;
      case '-' : cout<<n-m; break;
      case '*' : cout<<n*m; break;
      case '/' : if (m==0)
            cout<<"Division by zero!\n";
          else
            cout<<n/m;
            break;
      default:
            cout<<"Invalid operator";
   }
return 0;
 }
Output of the program:
Enter first value, arithmetic operator (+, -, * or /) and second value:
45+33
78