C++ nested switch statements

It is possible to have a switch as part of the statement sequence of an outer switch. Even if the case constants of the inner and outer switch contain common values, no conflicts will arise.

C++ specifies that at least 256 levels of nesting be allowed for switch statements.

Syntax

The syntax for a nested switch statement is as follows −

switch(ch1){case'A': 
      cout <<"This A is part of outer switch";switch(ch2){case'A':
            cout <<"This A is part of inner switch";break;case'B':// ...}break;case'B':// ...}

Example

#include <iostream>usingnamespace std;intmain(){// local variable declaration:int a =100;int b =200;switch(a){case100: 
         cout <<"This is part of outer switch"<< endl;switch(b){case200:
               cout <<"This is part of inner switch"<< endl;}}
   cout <<"Exact value of a is : "<< a << endl;
   cout <<"Exact value of b is : "<< b << endl;return0;}

This would produce the following result −

This is part of outer switch
This is part of inner switch
Exact value of a is : 100
Exact value of b is : 200

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *