Switch Case
We use switch case
when we have to check multiple conditions. It is similar to if else
but it is more efficient than if else
. It provided direct jump to the case statement
syntax of switch case :
switch(value)
{
case 1 :
// case 1 operations
break;
case 2 :
// case 2 operations
break;
case 3 :
// case 3 operations
break;
}
Example 1️⃣ :
#include <iostream>
using namespace std;
int main() {
int value;
cout<<"Enter Value:";
cin>>value;
switch(value)
{
case 1 :
cout<<"One";
break;
case 2 :
cout<<"Two";
break;
case 3 :
cout<<"Three";
break;
}
return 0;
}
Output :
Enter Value: 1
One
Explanation :
Line 5 : Declare variable which name has value
.
Line 6 : Enter value
from user.
Line 7 : store value using cin
object.
Line 9 : Use switch
keyword to check multiple condition.
Line 10 : {
open the body of switch case
.
Line 11 : case 1 :
and write the statement of case 1 and break the case 1 using break;
keyword.
Line 15 : case 2 :
and write the statement of case 2 and break the case 2 using break;
keyword.
Line 19 : case 3 :
and write the statement of case 3 and break the case 3 using break;
keyword.
Line 22 : }
close the body of switch case
.
Switch case without using break statement
Example 2️⃣ :
#include <iostream>
using namespace std;
int main() {
int value;
cout<<"Enter Value:";
cin>>value;
switch(value)
{
case 1 :
cout<<"One";
case 2 :
cout<<"Two";
case 3 :
cout<<"Three";
}
return 0;
}
Output :
Enter Value: 2
TwoThree
Explanation :
If there is no break
statement then the cases after the matched case, all case are exexuted other than default case.
Example 3️⃣ :
#include <iostream>
using namespace std;
int main() {
int value;
cout<<"Enter Value:";
cin>>value;
switch(value)
{
case 1 :
cout<<"One";
break;
case 2 :
cout<<"Two";
break;
case 3 :
cout<<"Three";
break;
default:
cout<<"Not Matched";
}
return 0;
}
Output :
Enter Value: 5
Not Matched
explanation :
The condition inside the switch(value)
is exexuted.
Then , it is matched with one of the case values, we execute that particular set of statements with it has matched.
After that case, we break out of switch.
If it does not match, we execute the default condition and come out of switch.
Example 4️⃣ :
#include <iostream>
using namespace std;
int main() {
int a,b;
cout<<"Enter Two Values:";
cin>>a>>b;
char op;
cout<<"Which operations do you want to perform:(+, -, /, *): ";
cin>>op;
switch(op)
{
case '-':
cout<<"a-b = "<<(a-b);
break;
case '+':
cout<<"a+b = "<<(a+b);
break;
case '*':
cout<<"a*b = "<<(a*b);
break;
case '/':
cout<<"a/b = "<<(a/b);
break;
default :
cout<<"Operation is not valid";
}
return 0;
}
Output :
Enter Two Values: 6,5
Which operations do you want to perform :(+, -, /, *): -
a-b = 1
**Explanation :
Line : 5 declare 2 variable which has a
and b
.
Line : 6 enter two values
from user.
Line : 8 declare op
variable.
Line : 10 Which operations do you want to perform:(+, -, /, *):
from user.
The condition inside the switch(op)
is exexuted.
Then , it is matched with one of the case values, we execute that particular set of statements with it has matched.
After that case, we break out of switch.
If it does not match, we execute the default condition and come out of switch.
Example 5️⃣ :
#include <iostream>
using namespace std;
int main() {
int a,b;
cout<<"Enter Two Values:";
cin>>a>>b;
char op;
cout<<"Which operations do you want to perform:(+, -, /, *): ";
cin>>op;
switch(op)
{
case "-";
cout<<"a-b = "<<(a-b);
break;
case "+";
cout<<"a+b = "<<(a+b);
break;
case "*";
cout<<"a*b = "<<(a*b);
break;
case "/";
cout<<"a/b = "<<(a/b);
break;
default :
cout<<"Operation is not valid";
}
return 0;
}
Output :
Enter Two Values: 9 9
Which operations do you want to perform :(+, -, /, *): #
Operation is not valid
**Explanation :
Line : 5 declare 2 variable which has a
and b
.
Line : 6 enter two values
from user.
Line : 8 declare op
variable.
Line : 10 Which operations do you want to perform:(+, -, /, *):
from user.
The condition inside the switch(op)
is exexuted.
Then , it is matched with one of the case values, we execute that particular set of statements with it has matched.
After that case, we break out of switch.
If it does not match, we execute the default condition and come out of switch.