Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Nested switch Statements

C Nested switch Statements

C Judgment C Judgment

You can place a switch as part of the statement sequence of an outer switch , that is, you can nest a switch if statement inside another switch switch statement. Even if the case constants of the inner and outer switch contain common values, there is no conflict.

Syntax

In the C language Nested switch statement syntax:



switch(ch1) {

   case 'A': 

      printf("这个 A 是外部 switch 的一部分" );

      switch(ch2) {

         case 'A':

            printf("这个 A 是内部 switch 的一部分" );

            break;

         case 'B': /* 内部 B case 代码 */

      }

      break;

   case 'B': /* 外部 B case 代码 */

}

Example

Example

#include

<

stdio.h

int

main

(

)

{

/*

Local variable definition

*/

int

a

=

100

;

int

b

=

200

;

switch

(

a

)

{

case

100

:

printf

(

"

This is part of the outer switch

\

n

"

)

;

switch

(

b

)

{

case

200

:

printf

(

"

This is part of the inner switch

\

n

"

)

;

}

}

printf

(

"

The exact value of a is %d

\

n

"

,

a

)

;

printf

(

"

The exact value of b is %d

\

n

"

,

b

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



这是外部 switch 的一部分

这是内部 switch 的一部分

a 的准确值是 100

b 的准确值是 200

C Judgment C Judgment