The switch statement in Java is a control flow statement used to execute one block of code from multiple options, based on the value of a single variable or expression. It is an alternative to the if-else if ladder when you are comparing a variable against multiple possible values.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
...
default:
// Code to execute if no case matches
}
Important Points
1. Expression: The value inside the switch must be of type byte, short, int, char, String, or an enum.
2. case Values: Each case represents a potential match for the expression. The case values must be constant and unique.
3. break Statement: It prevents the execution from “falling through” to the next case. Without break, all subsequent cases are executed.
4. default Case: This is optional and executed if none of the case values match the expression.
Example: Using switch with int
public class SwitchExample {
public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
}
}
Output: Tuesday
Example: Using switch with String
public class SwitchWithString {
public static void main(String[] args) {
String color = "green";
switch (color.toLowerCase()) {
case "red":
System.out.println("Stop!");
break;
case "yellow":
System.out.println("Get ready.");
break;
case "green":
System.out.println("Go!");
break;
default:
System.out.println("Invalid color.");
}
}
}
Output: Go!
Example: Omitting break statement
If you omit the break statement, execution continues to the next case until a break or the end of the switch is reached.
Example:
public class SwitchFallthrough {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
default:
System.out.println("Weekend");
}
}
}
Output:
WednesdayWeekend
When Default Case is executed
The default block is executed if no other case matches the expression. It is like the else in if-else.
Example:
public class ExampleSwitchDefault {
public static void main(String[] args) {
int number = 10;
switch (number) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
default:
System.out.println("No match found.");
}
}
}
Output: No match found
Using switch with enum
You can also use enum types in a switch statement.
public class SwitchWithEnum {
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
public static void main(String[] args) {
Day today = Day.TUESDAY;
switch (today) {
case MONDAY:
System.out.println("Start of the week!");
break;
case FRIDAY:
System.out.println("Almost weekend!");
break;
case SATURDAY:
case SUNDAY:
System.out.println("It's weekend!");
break;
default:
System.out.println("It's a weekday.");
}
}
}
Output: It’s a weekday.
Limitations of switch
1. switch cannot handle range-based conditions (<, >, etc.).
Example: switch cannot check if (age > 21) or if (age < 21).
2. It works only with primitive types (byte, short, int, char), String, or enum.