Krivalar Tutorials 
Krivalar Tutorials

Java switch case default statement


<<Previous

Next >>





switch case default statement

switch case is also another form of branching statement which can be used to execute different actions for different values as shown below. If none of the values mentioned in case match, then statements provided under 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 >>