how am i suppose to know what is switch???
Using switch case without teaching it!
we are here to help you.
Switch case is another selection control statement (like if-else)
Now, let’s first understand, what is selection control statement.
These are used when you have to execute only one part of the code out many different parts
if a specific condition is specified.
- If-else:
If(condition1){
//Perform addition
}
else if (condition2){
//Perform subtraction
}
else{
//don’t perform anything
}
Explanation:
If condition1 satisfies, addition would be performed,
If condition2 satisfies, subtraction
And if none of the condition 1 or condition 2 satisfies, do not perform anything.
2.switch case:
It is same as if else. But checks only for equality.
Int ch;
cin>>ch;
switch(ch){
case 2: cout<<“hello”;
break;
case 9: cout<<“hii”;
break;
default:cout<<“default”
}
Explanation:
Ch is a variable of type int.
If it’s value is 2, print hello
If it is 9, print hii
Default is like else of if-else
It is executed for considered cases that are mentioned.
It only checks for equality of a variable to a value.
Here, break is used to stop the compiler tonexecute any further inside switch block and exit it.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.