Introduction to Java Switch
The switch statement in Java is a control flow structure that simplifies decision-making by allowing multiple conditions to be evaluated in an organized and efficient way. Instead of writing multiple if…else if statements, you can use switch to handle several possible values of a single variable.
The switch statement enhances code readability and makes it easier to handle multiple cases with minimal code duplication.
Syntax of Switch Statement
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
...
default:
// Code to execute if no case matches
break;
}
Key Points About Switch Statement
- Expression:
- The switch expression must return an integer, string, or enumerated type (int, char, byte, short or String).
- Floating-point types like float or double are not allowed.
- Case Labels:
- Each
case
label must be a constant or literal. Variables or expressions are not allowed.
- Each
- Break Statement:
- The break statement prevents fall-through by stopping execution at the end of the matched case. If omitted, execution continues to the next case.
- Default Case:
- The default block executes if none of the cases match. It’s optional but recommended to handle unexpected values.
How Switch Works
- The switch expression is evaluated.
- Java compares the result with the values in each case.
- If a match is found, the corresponding block executes.
- If no match is found, the default block executes (if provided).
Examples of Switch Statement
Example 1: Basic Switch
public class SwitchExample {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
break;
}
}
}
Output:
Wednesday
Example 2: Switch with Strings
public class SwitchStringExample {
public static void main(String[] args) {
String fruit = "Apple";
switch (fruit) {
case "Apple":
System.out.println("Apples are red or green.");
break;
case "Banana":
System.out.println("Bananas are yellow.");
break;
case "Orange":
System.out.println("Oranges are orange.");
break;
default:
System.out.println("Unknown fruit.");
break;
}
}
}
Output:
Apples are red or green.
Example 3: Switch Without Break
public class SwitchWithoutBreak {
public static void main(String[] args) {
int number = 2;
switch (number) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Default Case");
}
}
}
Output:
Two
Three
Default Case
Explanation: Without the break statement, execution “falls through” to the next case, continuing until the end or until a break is encountered.
Example 4: Nested Switch
public class NestedSwitchExample {
public static void main(String[] args) {
String department = "Engineering";
int year = 3;
switch (department) {
case "Engineering":
switch (year) {
case 1:
System.out.println("Welcome to the first year of Engineering.");
break;
case 3:
System.out.println("You are in the third year of Engineering.");
break;
default:
System.out.println("Engineering: Year not specified.");
break;
}
break;
case "Medical":
System.out.println("Welcome to the Medical Department.");
break;
default:
System.out.println("Unknown Department.");
break;
}
}
}
Output:
You are in the third year of Engineering.
Advantages of Switch Statement
- Improved Readability:
- Makes the code cleaner compared to multiple if…else if statements.
- Efficient Execution:
- Switch statements are optimized at the bytecode level, making them faster than if…else for multiple conditions.
- Easy to Debug:
- The structure is easier to follow, making debugging simpler.
Disadvantages of Switch Statement
- Limited Data Types:
- Works only with specific data types (int, char, byte, short, String and enums).
- Risk of Fall-Through:
- Forgetting the break statement can lead to unintended behavior.
- Not Flexible:
- Cannot handle complex conditions like ranges (x > 5 && x < 10).