When a switch is a part of the statement sequence of another switch, then it is called as nested switch statement. The inner switch and the outer switch constant may or may not be the same.
The syntax of the nested switch statement is;
switch (expression)
{
case constant 1:
statement(s);
break;
switch(expression)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
.
.
.
default :
statement(s);
}
case constant 2:
statement(s);
break;
.
.
.
default :
statement(s);
}
The below program illustrates nested switch statement example. The outer switch checks for zero or non-zero and the inner switch checks for odd or even.
#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout<<"The Number is : " <<a <<endl;
switch (a)
{
case 0 :
cout<<"The number is zero" <<endl;
break;
default:
cout<<"The number is a non-zero integer" <<endl;
int b = a % 2;
switch (b)
{
case 0:
cout<<"The number is even" <<endl;
break;
case 1:
cout<<"The number is odd" <<endl;
break;
}
}
return 0;
}
The Number is : 8
The number is a non-zero integer
The number is even
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.