C# - Nested switch Statements



In c#, a nested switch statement refers to using a switch statement inside another switch statement. It is useful when working with multi-level decision-making.

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.

Syntax

Following is the syntax of the c# nested switch statement −

switch(ch1) {
   case 'A':
   Console.WriteLine("This A is part of outer switch" );
   
   switch(ch2) {
      case 'A':
         Console.WriteLine("This A is part of inner switch" );
         break;
      case 'B': /* inner B case code */
   }
   break;
   case 'B': /* outer B case code */
}

Rules for Using Nested switch Statements

A nested switch statement is a switch statement inside another switch. Here are some simple rules to follow when using them:

  • The expression inside each switch must be an integral type (e.g., int, char) or an enumerated type.
  • Each switch can have multiple case labels, and the case values must match the data type of the switch expression.
  • Case values must be constants or literals (e.g., numbers or characters). Variables are not allowed as case values.
  • When a case matches, the code inside that case runs until a break statement is reached.
  • Every case must end with a break statement to stop execution. Missing a break will cause a compile-time error.
  • A switch statement can be nested, meaning one switch can be placed inside another within a case block.
  • Both the outer and inner switch statements can have a default case, which runs when no other case matches.

Example: Using of Nested Switch Statement

In this example, we are demonstrating how a nested switch statement works in C# −

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 outer switch ");
            
            switch (b) {
               case 200:
               Console.WriteLine("This is part of inner switch ");
               break;
            }
            break;
         }
         Console.WriteLine("Exact value of a is : {0}", a);
         Console.WriteLine("Exact value of b is : {0}", b);
         Console.ReadLine();
      }
   }
} 

Output

When the above code is compiled and executed, it produces 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

Example: Nested Switch for Department and Role Selection

Here, in this example, we use the nested switch statement to display the roles and responsibilities If the department and role are given −

using System;
public class Example {
   public static void Main() {
      // Define department
      string department = "IT";

      // Define Role
      string role = "Manager";

      switch (department) {
      case "IT":
         switch (role) {
         case "Developer":
            Console.WriteLine("IT - Developer: Responsible for coding.");
            break;
         case "Tester":
            Console.WriteLine("IT - Tester: Ensures software quality.");
            break;
         case "Manager":
            Console.WriteLine("IT - Manager: Oversees IT projects.");
            break;
         default:
            Console.WriteLine("Invalid IT Role!");
            break;
         }
         break;

      case "HR":
         switch (role) {
         case "Recruiter":
            Console.WriteLine("HR - Recruiter: Manages hiring.");
            break;
         case "Trainer":
            Console.WriteLine("HR - Trainer: Conducts training sessions.");
            break;
         case "Coordinator":
            Console.WriteLine("HR - Coordinator: Handles HR operations.");
            break;
         default:
            Console.WriteLine("Invalid HR Role!");
            break;
         }
         break;

      case "Finance":
         switch (role) {
         case "Accountant":
            Console.WriteLine("Finance - Accountant: Manages financial records.");
            break;
         case "Auditor":
            Console.WriteLine("Finance - Auditor: Conducts financial audits.");
            break;
         case "Analyst":
            Console.WriteLine("Finance - Analyst: Analyzes financial data.");
            break;
         default:
            Console.WriteLine("Invalid Finance Role!");
            break;
         }
         break;

      default:
         Console.WriteLine("Invalid Department!");
         break;
      }
   }
}

Output

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

IT - Manager: Oversees IT projects.
Advertisements