What is default switch case ?
Printable View
What is default switch case ?
The case statement and default statement can occur in any order of the switch statement this clause is an optional clause that is matched none of the constants in the case statement can be matched.
The default clause is an optional clause that is matched if none of the constants in the case statements can be matched.
The switch and case keywords evaluate expression and execute any statement associated with constant expression whose value matches the initial expression.
A switch statement can have an optional default case, which must appear at the end of the switch.
The switch statement is n selection control mechanism used to allow the value of the variable.
In a switch statement, default case is executed when no other switch condition matches. Default case is an optional case .
It can be declared only once all other switch cases have been coded.
In the below example, when score is not 1 or 2, default case is used.
Code: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");
}
}
}
The switch and case keywords evaluate expression and execute any statement associated with constant-expression whose value matches the initial expression.