Switch Statement In C++


Syntax :

Switch ( variable )
{
case constant-value1 :       //  when variable value match with constant-value1

// code

break ;

case constant-value2 :      // when variable value match with constant-value2

// code

break ;

default :  // when variable value not match with constant-value then default block execute

// code

}
Example :

#include <iostream>
using namespace std;
int main() {
int a ;
cin>>a ;

switch ( a )
{
case 1 :
cout<<" a = 1" ;
break;                    // break is use to terminate switch block
case 2 :
cout<<" a = 2" ;
break;
case 3 :
 cout<<" a = 3" ;
break;                  

default :
cout<<"a is not match with any case"  ;

}

return 0 ;
 }

No comments:

Post a Comment