<<Previous
Next >>
switch case default statement
switch case is a form of branching statement used to execute actions based on values. If no values specified in case match, then the statements provided in default will execute. Example Switch Case 1:
public class SwitchCase {
public static void main(String a[]){
int sides=4;
switch(sides ){
case 1:
System.out.println("Line");
break;
case 2:
System.out.println("Angle");
break;
case 3:
System.out.println("Triangle");
break;
case 4:
System.out.println("Quadrilateral");
break;
default:
System.out.println("Polygon");
break;
}
}
}
The above code will print:
Quadrilateral
Please note that break is used in each of the case clause. This causes the execution to break outside of switch and execute the next statement that follows the switch block Example Switch Case 2:
public class SwitchCaseNoBreak {
public static void main(String a[]){
String sides="JAPAN";
switch(sides ){
case "INDIA":
System.out.println("TAJ MAHAL");
case "AMRITSAR":
System.out.println("Golden Temple");
break;
case "JAPAN":
System.out.println("Todaiji Temple");
case "KYOTO":
System.out.println("Kyoto Imperial Palace");
break;
case "GREECE":
System.out.println("Kyoto Imperial Palace");
break;
case "EGYPT":
System.out.println("Pyramids");
break;
case "AMERICA":
System.out.println("Pyramid of the Sun, Mexico");
case "COLUMBIA":
System.out.println("San AgustÃn");
break;
}
}
}
The above code will print:
Todaiji Temple
Kyoto Imperial Palace
<<Previous
Next >>