Java Nested Switch Example


A switch statement will match the value in switch() with all the values in the list, if match it will execute that statement, otherwise, it will execute the default statement.

In nested switch, We can use a switch inside another switch statement.

Program
public class NestedSwitch { public static void main(String args[]) { String a1="Good"; String a2="Morning"; switch(a1) { case "Good" : switch(a2) { case "Morning": System.out.println("Good Morning"); break; case "Night": System.out.println("Good Night"); break; } break; case "Bad" : switch(a2) { case "Morning": System.out.println("Good Morning"); break; case "Night": System.out.println("Good Night"); break; } break; default : System.out.println("Wrong input"); } } }
Input
Output