Advertisement
// C++ switch example
#include<iostream>
int main() // main method starts
{
int i=1;
switch(i)
{
case 1:
cout<<"value is 1";
break;
case 2:
cout<<"value is 2";
break;
case 3:
cout<<"value is 3";
break;
default:
cout<<"This is default";
}
} // main method ends and so does the program
value is 2
// C++ switch example with matching case
#include<iostream>
int main() // main method starts
{
char i='A';
switch(i)
{
case 'x':
cout<<"char value is x";
break;
case 'E':
cout<<"char value is A";
break;
case 'C':
cout<<"char value is C";
break;
default:
cout<<"This is default";
}
return 0;
} // main method ends and so does the program
This is default
Advertisement
// C - switch statement example with no break statement
#include<iostream>
using namespace std;
int main() // main method starts
{
int i=20;
switch(i)
{
case 10:
cout<<"value is 10" <<"\n";
case 20:
cout<<"value is 20" <<"\n";
case 30:
cout<<"value is 30" <<"\n";
default:
cout<<"This is default";
}
return 0;
} // main method ends and so does the program
value is 20
value is 30
This is default
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement