switch case in Java
What is the default switch case?
870
27-Aug-2019
Shrikant Mishra
27-Aug-2019Ans: In a switch statement, the default case is executed when no other switch condition matches. The default case is an optional case. It can be declared only once all other switch cases have been coded.
In the below example, when the score is not 1 or 2, the default case is used.
public class switchExample {int score = 4;
public static void main(String args[]) {
switch (score) {
case 1:
system.out.println("Score is 1");
break;
case 2:
system.out.println("Score is 2");
break;
default:
system.out.println("Default Case");
}
}
}