What is Switch Statement?
Switch statement is used to execute one block from multiple options.
Switch Syntax
switch(expression){
case value:
code
break;
default:
code;
}
Example
int day = 2;
switch(day){
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid");
}
Default Case
default block runs when no case matches.
Break Keyword
break stops execution after matching case.
Switch Without Break
int value = 1;
switch(value){
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
}
Output
One Two