Java Switch Statement
The Java switch statement executes one statement from multiple conditions. It is like if-else-if
ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.In other words, the switch statement tests the equality of a variable against multiple values.
SYNTAX:-
switch(expression){
case value1:
break;
case value2;
break;
......
default:
code to be executed if all cases are not matched;
}
EXAMPLE:-
public class example{
public static void main(String args[]){
int num=20;
switch(num){
case 10: System.out.println("10");
break;
case 20: System.out.println("20");
break;
case 30: System.out.println("30");
break;
default: System.out.println("Not in 10,20 or 30");
}
}
}
0 Comments