C# Nested switch Statements

C# conditionals

You can use aswitchas an externalswitchpart of the statement sequence, that is, it can be in aswitchUse another within a statementswitchstatement. Even if the case constants of the inner and outer switch contain common values, there is no contradiction.

Syntax

In C#Nested switchThe syntax of the statement:

switch(ch1)
{
   case 'A':
      printf(This A is part of the outer switch );
      switch(ch2)
      {
         case 'A':
            printf(This A is part of the inner switch );
            break;
         case 'B': /* Internal B case code */
      }
      break;
   case 'B': /* External B case code */
}

Example

Example

using System;

namespace DecisionMaking
{
   
    class Program
    {
        static void Main(string[] args)
        {
            int a = 100;
            int b = 200;

            switch (a)
            {
                case 100:
                    Console.WriteLine("This is part of the outer switch");
                    switch (b)
                    {
                        case 200:
                        Console.WriteLine("This is part of the inner switch");
                        break;
                    }
                    break;
            }
            Console.WriteLine("The exact value of a is {0}", a);
            Console.WriteLine("The exact value of b is {0}", b);
            Console.ReadLine();
        }
    }
}

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

这是外部 switch 的一部分
这是内部 switch 的一部分
a 的准确值是 100
b 的准确值是 200

C# conditionals

other extensions