The switch Statement
The switch statement is used when you want to compare a single variable against multiple constant values and run a block of code based on a match.
When to Use switch:
- You have many if-else conditions comparing the same variable.
- Each condition checks equality with a constant value (like numbers, characters, strings).
- It makes code more readable and organized than multiple if-else-if.
Key Points:
- The switch checks the value of a variable once, and compares it against multiple case labels.
- Use break to exit the switch after a match (avoids fall-through).
- The default block is optional and runs if no case matches.
Syntax:
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
...
default:
// default code
}
- The expression used in a switch must be of a specific type:
It can only be a byte, short, int,
char, enum, or String (String is allowed from
Java 7 onwards).
! You cannot use types like float, double, or boolean in a switch.
- If you don’t use break after a case, Java will keep running the next case
statements — even if
they don’t match.
This behavior is called fall-through.
! To stop the flow after a case block runs, you must use break.
Example 1: With break (Normal Usage)
class ShikshaSanchar {
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;
default:
System.out.println("Other day");
}
}
}
Output:
Tuesday
Explanation:
- Since day = 2, it matches case 2.
- "Tuesday" is printed.
- break stops the switch, so no other cases are executed.
Example 2: Without break (Fall-Through Behavior)
class ShikshaSanchar {
public static void main(String[] args) {
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
default:
System.out.println("Other day");
}
}
}
Output:
Tuesday
Wednesday
Other day
Explanation:
- case 2 matches and prints "Tuesday".
- There is no break, so Java continues executing the next cases.
- "Wednesday" and "Other day" are also printed — even though they don’t match.
Multiple Cases, One break (Grouped Cases)
class ShikshaSanchar {
public static void main(String[] args) {
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("Weekday");
break;
default:
System.out.println("Invalid day");
}
}
}
Output:
Weekend
Explanation:
- case 6 and case 7 are grouped to print "Weekend".
- They share the same code block before the break, which avoids repeating the same code.
- This is a clean way to handle multiple values with the same result.
Summary:
- Used to compare one variable with multiple constant values.
- Each case checks for equality.
- Use break to exit after a match (prevents fall-through).
- default runs if no match is found (optional).
- Works with: byte, short, int, char, enum, and String (Java 7+).
- Doesn't support: boolean, float, double.
Cleaner alternative to multiple if-else-if.