Latest web development tutorials

C # nested switch statement

C # judge C # judge

You can put aswitch as part of a sequence of statements of the external switch,that can be used in anotherswitchstatement within aswitchstatement. Even in case of internal and external switch constant contains common values, there is no contradiction.

grammar

C #nested switch statement syntax:

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

Examples

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 an external switch.");
                    switch (b)
                    {
                        case 200:
                        Console.WriteLine ( "This is an internal switch part");
                        break;
                    }
                    break;
            }
            Console.WriteLine ( "a precise value is {0}", a);
            Console.WriteLine ( "b the exact value is {0}", b);
            Console.ReadLine ();
        }
    }
} 

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

This is an external switch which is part of the exact value of the internal switch is part of a 100
b The exact value is 200

C # judge C # judge