Nested Switch in Java

In this article, we will discuss about nested switch in Java with examples.

When a switch statement contains further switch statements inside it, then it is called a nested switch. Inside the nested switch statement, we can give multiple cases and a default case. The inner switch statement must be part of any outer case statement. We cannot use the inner switch statements outside the cases.

Nested-Switch-In-Java

Syntax of Nested Switch

switch(n)

{

   // code will be executed if n = 1;

   case 1:

   // Nested switch

   switch(value)

   {

          // code to be executed if value = 5

          case 5:

                statement 1;

                break;

          // code will be executed if value = 10

          case 10:

                statement 2;

                break;

           // code will be executed if value = 15

           case 15:

                statement 3;

                break;

         // code will be executed if the value doesn't match any cases

         default:

               statement 4;

     }

       break;

     // code will be executed if n = 2;

    case 2:

        statement 2;

        break;

     // code will be executed if n = 3;

     case 3:

         statement 3;

          break;

    // code will be executed if n does not match with any other cases

     default:

}

Nested Switch Program

public class start {
    public static void main(String[] args) {
        //Declaring a variable for switch expression
        int year = 3;
        int marks = 80;
        
        switch(year) //Switch expression
        {
            //Case statements
            case 1: System.out.println("First year students");
                break;
            case 2: System.out.println("Second year students");
                break;
            case 3: switch(marks) {
                        case 50: System.out.println("You are not eligible for scholarship");
                            break;
                        case 80: System.out.println("Congrats!!!!! you are eligible for scholarship");
                            break;
                    }
                break;
            //Default case statement
            default: System.out.println("Please enter valid year");
        }
    }
}

Output:

Congrats! You are eligible for the scholarship.

Benefits of Nested Switch

  • Easy to maintain.
  • More efficient than if-else.
  • Easy to debug.
  • It performs execution at a faster pace.

Limitations of Nested Switch

  • It doesn’t work with ranges.
  • A switch can take inputs only in numbers and character, it doesn’t take strings.
  • There can be multiple cases inside a single switch statement.

Note that if we don’t use the break statement after each case inside the switch block, it prints all the cases starting from the matching case to the default case. Also, if no case matches the given condition, then by default the iterator prints the statements under the default case. It is not a necessity to use a break statement inside the default case.

So, this is all about the nested switch statement in Java.

1 thought on “Nested Switch in Java”

Leave a Comment

Your email address will not be published. Required fields are marked *