Search Your Topic

config
Thursday, 08 June 2017 08:22

C++ switch

Written by
Rate this item
(0 votes)

The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.

 
  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. }    

Cpp Switch 1


C++ Switch Example

 
  1. #include <iostream>  
  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<<"Not 10, 20 or 30"break;    
  13.           }    
  14.     }    

Output:

Enter a number:
10
It is 10

Output:

Enter a number:
55
Not 10, 20 or 30
Read 239 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ if-else C++ For Loop »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.