Switch Case in C plus plus

Switch case in C plus plus is a statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.

Syntax for Switch Case in C plus plus
  1. switch(expression) {      
  2. case value1:      
  3.  //code to be executed;      
  4.  break;    
  5. case value2:      
  6.  //code to be executed;      
  7.  break;    
  8. ……      
  9.       
  10. default:       
  11.  //code to be executed if all cases are not matched;      
  12.  break;    
  13. }    

C++ Switch Example

 
  1. #include   
  2. using namespace std;  
  3. int main () {  
  4.        int num;  
  5.        cout<<“Enter a number to check grade:”;    
  6.        cin>>num;  
  7.            switch (num)    
  8.           {    
  9.               case 10: cout<<“It is 10”break;    
  10.               case 20: cout<<“It is 20”break;    
  11.               case 30: cout<<“It is 30”break;    
  12.               default: cout<<“No wrong input,chose: 10, 20 or 30”break;    
  13.           }    
  14.     }    

Output:

Enter a number:
10
It is 10

Output:

Enter a number:
55
No wrong input,Chose: 10, 20 or 30