C# - Example of Nested Switch Statement

C# nested switch statement example: Here, we are going to learn how to use nested switch statement (switch within switch) in C# programming language?
Submitted by IncludeHelp, on April 09, 2019 [Last updated : March 17, 2023]

Nested Switch Statement

switch statement in C# allows checking a variable/value with a list of values (cases) and executing the block associated with that case.

When we use switch statement within another switch statement (a case statement(s)) i.e. switch statement within another switch statement, we can say it is an example of a nested switch statement.

Synatx

//outer switch
switch(variable/expression)
{
    case <case_value1>:
    statement(s);
    //inner switch
    switch(variable/expression)
    {
        case <case_value1>:
            statement(s);
            break;
        case <case_value2>:
            statement(s);
            break;
        default:
            statement(s);
            break;
    }
    break;
    case <case_value2>:
        statement(s);
        break;
    default:
        statement(s);
        break;
}

C# program to demonstrate the example of nested switch statement

Here, we have 3 cases:
(Case 1) Using another switch statement, that will give the color name based on the user input (color code – example "R/r" for "Red", "G/g" for "Green", ...)
(Case 2) and Case 3) will print simple message.

// C# - Example of Nested Switch Statement

using System;
using System.IO;
using System.Text;

namespace IncludeHelp {
  class Test {
    // Main Method 
    static void Main(string[] args) {
      int number;

      //input a number
      Console.Write("Enter a number (1-3): ");
      number = Convert.ToInt32(Console.ReadLine());

      //outer switch statement
      switch (number) {
      case 1:
        //using another case 
        //it will input R,G or B and print the color
        char color;
        Console.Write("Enter color value (R/G/B): ");
        color = Console.ReadLine()[0];
        //validating it using switch case
        //inner switch
        switch (color) {
        case 'R':
        case 'r':
          Console.WriteLine("You've choesn \"Red\" color");
          break;
        case 'G':
        case 'g':
          Console.WriteLine("You've choesn \"Green\" color");
          break;
        case 'B':
        case 'b':
          Console.WriteLine("You've choesn \"Blue\" color");
          break;
        default:
          Console.WriteLine("Invalid color code");
          break;
        }
        break;

      case 2:
        Console.WriteLine("Input is 2");
        break;

      case 3:
        Console.WriteLine("Input is 3");
        break;
      default:
        Console.WriteLine("Invalid number");
        break;
      }

      //hit ENTER to exit the program
      Console.ReadLine();
    }
  }
}

Output

First run:
Enter a number (1-3): 1
Enter color value (R/G/B): B
You've choesn "Blue" color

Second run:
Enter a number (1-3): 1
Enter color value (R/G/B): r
You've choesn "Red" color

Third run:
Enter a number (1-3): 1
Enter color value (R/G/B): x
Invalid color code

Fourth run:
Enter a number (1-3): 3
Input is 3

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.