Java - Decision Control and looping statement - Switch Tutorial
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.
public class SwitchCase {
public static void main(String args[]) {
switch('C') {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("Failed");
default :
System.out.println("Invalid grade");
}
}
}
Nested Switch
We can use a switch inside another switch statement.
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");
}
}
}