how to apply switch case in c++ code example
Example 1: c++ switch multiple cases
#include <iostream>
using namespace std;
int main() {
// variable declaration
int input;
switch(input){
case 1: case 2: case 3: case 4:
//executes if input is 1, 2, 3, or 4
break;
case 5: case 6: case 7:
//executes if input is 5, 6, or 7
break;
default:
//executes if input isn't any of the cases
}
return 0;
}
Example 2: switch c++
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}