c++ nested switch statements
Nested Switch Statements in C++
#include <iostream>
using namespace std;
int main() {
int x = 2;
int y = 1;
switch (x) {
case 1:
switch (y) {
case 1:
cout << "x is 1, y is 1";
break;
case 2:
cout << "x is 1, y is 2";
break;
}
break;
case 2:
switch (y) {
case 1:
cout << "x is 2, y is 1";
break;
case 2:
cout << "x is 2, y is 2";
break;
}
break;
}
return 0;
}
- Include the necessary header files and namespace.
- Define the main function.
- Declare variables x and y and initialize them.
- Start the outer switch statement with the variable x as its expression.
- Begin the first case of the outer switch statement with the value 1.
- Start the inner switch statement with the variable y as its expression.
- Define the cases inside the inner switch statement.
- Break out of the inner switch statement.
- Break out of the outer switch statement.
- Define the second case of the outer switch statement with the value 2.
- Start the inner switch statement with the variable y as its expression.
- Define the cases inside the inner switch statement.
- Break out of the inner switch statement.
- Break out of the outer switch statement.
- End the main function and return 0.