The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++.
c++ Simple Example
#include <iostream>
using namespace std;
int main () {
int num;
cout<<"Enter a number to check grade:";
cin>>num;
switch (num)
{
case 10: cout<<"It is 10"; break;
case 20: cout<<"It is 20"; break;
case 30: cout<<"It is 30"; break;
default: cout<<"Not 10, 20 or 30"; break;
}
}
Another example of switch simple calculator
int a,b;
char operate;
cout<<"What operation would you perform( +,/,x,- ) ";
cin>>operate
cout<<"Enter the first Number :";
cin>>a;
cout<<"Enter the first Number :";
cin>>b;
switch (operate)
{
case '+':
cout<<"You Perform Addition, Answer is = "<<a+b<<endl;
break;
case '-':
cout<<"You Perform Subtraction, Answer is = "<<a-b<<endl;
break;
case 'x':
cout<<"You Perform Multiplication, Answer is = "<<a*b<<endl;
break;
case '/':
cout<<"You Perform Division, Answer is = "<<a/b<<endl;
break;
default:
cout<<"We are Unable to operate the ` "<<operate<<" ` \n";
break;
}