C++ Switch Case Statement

A switch statement in C++ allows a variable or value of an expression to be tested for equality against a list of possible case values and when match is found, the block of code associated with that case is executed. It is similar to if..else ladder statement, but the syntax of switch statement is more readable and easy to understand.

Syntax of Switch Statement

switch(expression) {
    case constant1 :
       // statements to be executed when value of expression equals constant1
       break;
    case constant2 :
       // statements to be executed when value of expression equals constant2
       break;
    case constant3 :
       // statements to be executed when value of expression equals constant3
       break;
    default :   
       // Default block will execute when none of the case matches }
C++ Switch Statement Control Flow Diagram

Important Points about Switch Statement in C++

  • Switch case performs only equality check of the value of expression/variable against the list of case values.

  • Case constants must be unique. We cannot write two case blocks with same constants.

  • The expression in switch case must evaluates to return an integer, character or enumerated type.

  • You can use any number of case statements within a switch.

  • One switch statement can have maximum of one default label. Default block can be placed anywhere in switch statement.

  • The data type of the value of expression must be same as the data type of case constants.

  • The break statement is optional. The break statement at the end of each case cause switch statement to exit. If break statement is not used, all statements below that case statement are also executed until it found a break statement.

  • The default code block gets executed when none of the case matches with expression. default case is optional and doesn't require a break statement.

C++ Switch Statement Example Program

#include <iostream>
using namespace std;
  
int main(){
   int n;
    
   cout << "Enter a number between 1 to 6\n";
   cin >> n;
    
   switch(n){
       case 1 :
           /* following code will execute when n == 1 */
           cout << "ONE\n";
           break;
       case 2 :
           /* following code will execute when n == 2 */
           cout << "TWO\n";
           break;
       case 3 :
           /* following code will execute when n == 3 */
           cout << "THREE\n";
           break;
       case 4 :
       case 5 :
       case 6 :
           /* following code will execute when n==4 or n==5 or n==6*/
           cout << "FOUR, FIVE OR SIX\n";
           break;
       default :
           /* following code will execute when n < 1 or n > 6 */
           cout << "INVALID INPUT\n";
   }
   
   cout << "Always gets printed\n";

   return 0;
}

Output
Enter a number between 1 to 6
3
THREE
Always gets printed
Enter a number between 1 to 6
6
FOUR, FIVE OR SIX
Always gets printed
Enter a number between 1 to 6
8
INVALID INPUT
Always gets printed

Advantages and Disadvantages of Switch Case in C++

Advantages of Switch Case
  • The complexity of the program increases as the number of constants to compare increases. Switch statement provides a way to write easy to manage and readable code.
  • Switch statement provides a default case handler when no case matches.
  • We can nest switch statements like if else statements.
  • The syntax of switch case is easy to understand and more readable.
Disadvantages of Switch Case
  • Switch statements can only be used to perform equality comparison, we cannot perform conditional checks using relational or logical operators like >, <, >==, &&, || etc.