🔀 switch-case Statement in Java
When you have multiple conditions to check, writing a long chain of if-else-if statements can make your code look messy 😵.
👉 That’s where the switch-case statement comes in!
It’s like a menu card 📝 → you choose one option, and the program executes the matching block.
🧐 What is switch-case in Java?
A switch statement is used to execute one block of code out of many options, based on the value of a variable or expression.
👉 Syntax:
switch(expression) {
case value1:
// Code if expression == value1
break;
case value2:
// Code if expression == value2
break;
...
default:
// Code if no case matches
}
🔍 How it Works
1️⃣ The expression inside switch() is evaluated.
2️⃣ The value is compared with each case.
3️⃣ If a match is found → that block executes.
4️⃣ break stops execution after a match (otherwise code will “fall through”).
5️⃣ If no match is found → default block runs.
✅ Example 1: Day of the Week
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;
case 6: System.out.println("Saturday"); break;
case 7: System.out.println("Sunday"); break;
default: System.out.println("Invalid day");
}
Output:
Wednesday
🚀 Why use switch-case?
✔️ Cleaner than long if-else-if chains
✔️ Easy to read & maintain
✔️ Works great for menu-driven programs, enums, strings, and numbers
✔️ Boosts performance for multiple comparisons
⚡ Popular Examples of switch-case
1️⃣ Calculator Program
int a = 10, b = 5;
char operator = '+';
switch(operator) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(a / b); break;
default: System.out.println("Invalid Operator");
}
2️⃣ Student Grade
char grade = 'B';
switch(grade) {
case 'A': System.out.println("Excellent!"); break;
case 'B': System.out.println("Good Job!"); break;
case 'C': System.out.println("Keep Trying!"); break;
default: System.out.println("Invalid Grade");
}
3️⃣ String in switch-case (Java 7+)
String fruit = "Apple";
switch(fruit) {
case "Apple": System.out.println("Red Fruit"); break;
case "Banana": System.out.println("Yellow Fruit"); break;
case "Grapes": System.out.println("Green Fruit"); break;
default: System.out.println("Unknown Fruit");
}
💡 Pro Tips
✅ Always use break to avoid fall-through (unless you want it intentionally).
✅ Use default to handle unexpected values.
✅ From Java 7 onwards, you can use String in switch-case.
✅ From Java 12 onwards, Java introduced Switch Expressions with -> for more concise code.
📌 Example:
int day = 5;
String result = switch(day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println(result);
🔑 Difference between if-else and switch-case
| Feature | if-else | switch-case |
|---|---|---|
| Usage | Complex conditions (>, <, &&, | |
| Readability | Harder for many conditions | Cleaner for multiple choices |
| Supported Types | boolean, relational operators | int, char, String, enum |
✅ In short:
The switch-case statement is like a decision menu 🍴 for your program. It makes your code cleaner, faster, and easier to maintain, especially when dealing with multiple options.