c++ switch statement with or code example
Example 1: switch c++
switch(expression) {
case 1:
//equivalent to if(expression == 1){//do someting...}
//do something...
break;
//if case 1 is true the rest of the statments arn't
//evaluated because of the break
case 45:
//equivalent to else if(expression == 45){//do someting...}
//do something...
break;
// you can have any number of case statements and default has to be last
default :
// equivalent to else{//do someting...}
//do something...
}
switch(expression) {
case 1:
//equivalent to if(expression == 1){//do someting...}
//do something...
case 45:
//equivalent to if(expression == 45){//do someting...}
//do something...
default :
//always runs if there are no breaks in any of the cases
//do something...
}
//modification of answer by Homeless Hoopoe
Example 2: how to make a switch case statement in c++
using namespace std;
int main(){
int num = 5;
switch(num + 2) {
case 1:
cout << "Case1: Value is: " << num << endl;
case 2:
cout << "Case2: Value is: " << num << endl;
case 3:
cout << "Case3: Value is: " << num << endl;
default:
cout << "Default: Value is: " << num << endl;
}
return 0;
}