switch
Keyword in Java
The switch
keyword in Java is used for decision-making. It's an alternative to writing many if-else-if
statements, and it allows you to test a single expression against multiple possible values. When readability and clarity matter, switch
often makes your code cleaner and more elegant.
Basic Syntax of switch
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
...
default:
// default block
}
Here’s what each part means:
- expression: A variable or value evaluated once, usually
int
,char
,String
(since Java 7), orenum
. - case: Each
case
is checked against the value of the expression. - break: Exits the switch block. Without it, execution "falls through" to the next case.
- default: Optional. Executes if no match is found.
Example 1: Integer-Based switch
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Another day");
}
Wednesday
Since day
is 3, the third case matches and prints "Wednesday". The break
prevents the next cases from being executed.
Example 2: What Happens Without break?
int rating = 4;
switch (rating) {
case 5:
System.out.println("Excellent");
case 4:
System.out.println("Good");
case 3:
System.out.println("Fair");
default:
System.out.println("Needs improvement");
}
Good
Fair
Needs improvement
Without break
, the switch statement "falls through" and executes all subsequent cases. That’s why it prints everything starting from case 4
.
Example 3: Using String in switch (Java 7+)
String role = "admin";
switch (role) {
case "admin":
System.out.println("Welcome Admin");
break;
case "editor":
System.out.println("Welcome Editor");
break;
default:
System.out.println("Role not recognized");
}
Welcome Admin
Java allows String
types in switch
statements starting with Java 7, making it useful for text-based conditions.
Example 4: switch with char
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done");
break;
case 'C':
System.out.println("You passed");
break;
default:
System.out.println("Invalid grade");
}
Well done
Example 5: switch with enum
enum Direction {
NORTH, SOUTH, EAST, WEST
}
Direction dir = Direction.EAST;
switch (dir) {
case NORTH:
System.out.println("You are heading North");
break;
case EAST:
System.out.println("You are heading East");
break;
}
You are heading East
enum
types make switch
more expressive and type-safe, often used in game dev or control systems.
Best Practices for switch
- Always include
break
unless fall-through is intentional. - Use
default
to handle unexpected input. - Consider using
enum
for finite state values to improve readability and safety. - Be cautious when using
String
—it’s case-sensitive.
When Not to Use switch
If you need to compare ranges or perform more complex conditions, if-else-if
blocks may be more appropriate.
Comments
Loading comments...