Home | | Computer Science 11th std | C++ Nested switch

Flow of Control | C++ - C++ Nested switch | 11th Computer Science : Chapter 10 : Flow of Control

Chapter: 11th Computer Science : Chapter 10 : Flow of Control

C++ Nested switch

When a switch is a part of the statement sequence of another switch, then it is called as nested switch statement.

Nested switch

 

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.

Illustration 10.10 C++ program to check for zero or non-zero and odd or even using  nested switch statement

#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;

}

Output

The Number is : 8

The number is a non-zero integer

The number is even


Tags : Flow of Control | C++ , 11th Computer Science : Chapter 10 : Flow of Control
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 10 : Flow of Control : C++ Nested switch | Flow of Control | C++


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.