• Have Any Question?
  • contact@eitworld.com

cpp nested switch





C++ nested switch statements

When we use switch inside switch is called as nested switch statements means switch-case statement inside the other switch-case statement. The inner and outer switch() case constant may be same or may be different. Here eitworld.com provides the C++ Nested Switch Statements in easy way. Nested switch statements and functions is very important concept in the C++ programming so eitworld.com introduced the study material on the nested switch statements C++ for the easy and simple learning.

    • We can use one switch-case statement inside the other switch-case statement.
    • The inner and outer switch() case constant may be same or may be different.
    • The basic syntax of nested switch-case statement is as follows:

Syntax

switch(expression1)

{

case constant A:

statements;

switch(expression2)

{

case constant A:

statements;

break;

case constant B:

statements;

break;

default:

statements;

break;

}

break;

case constant B:

statements;

break;

default:

statements;

break;

}

  • Here is an example of nested switch-case statement as follows:

Example

#include<iostream.h>

#include<conio.h>

int main()

{

int choice1,choice2;

cout<<“n Enter your first choice:”;

cin>>choice1;

switch(choice1)

{

case 1:

cout<<“nOuter switch-case statement:Your first choice is 1”;

cout<<“n Enter your second choice:”;

cin>>choice2

switch(choice2)

{

case 1:

cout<<“nInner switch-case statement:Your second choice is 1”;

break;

case 2:

cout<<“nInner switch-case statement:Your second choice is 2”;

break;

case 3:

cout<<“nInner switch-case statement:Your second choice is 3”;

break;

default:

cout<<“nNo match found”;

break;

}

break;

case 2:

cout<<“nOuter switch-case statement:Your first choice is 2”;

break;

case 3:

cout<<“nOuter switch-case statement:Your first choice is 3”;

break;

default:

cout<<“nNo match found”;

break;

}

getch();

return 0;

}

Output

The output of the above program is as follows:

Enter your first choice: 1

Outer switch-case statement:Your first choice is 1

Enter your second choice:2

Inner switch-case statement:Your second choice is 2



Real Time Web Analytics