Nested switch:
We can use a switch as part of the statement sequence of an outer switch. This is called nested switch. We know that each switch defines its own block of code but case constants of outer switch have no conflicts with the inner switch's case constants.
General syntax:
switch (value)
{
case 1:
switch (value)
{
case 1:
statement(s)
break;
.
.
case n:
statement (n)
break;
default:
statement
}
case 2:
statement(s)
break;
.
.
case n:
statement (n)
break;
default:
statement
}
Here, value can be a variable or any constant value. Remember case 1 of outer switch have no conflicts with case 1 of inner switch. If the value matches with case 1 (outer switch's) then inner switch will be executed otherwise it will be bypassed.
- Click here to see a program
No comments:
Post a Comment