Today I learned about another important decision-making concept in Java π Switch Case Statement.
It is very useful when we need to compare one value against multiple options.
πΉ What is Switch Case?
A switch statement allows us to select one block of code from multiple choices.
π Instead of writing multiple if-else, we can use switch for better readability.
π§ Syntax
switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// default code
}
π Important Points
β switch works with:
int
char
String
enum
β Not supported: float, double, boolean
πΈ What is break?
π break is used to stop execution of switch
Without break, Java will continue executing next cases (fall-through) β οΈ
πΈ What is continue?
π continue is NOT used inside switch directly
β It is used inside loops
β But can be used in switch only if switch is inside a loop
πΈ Switch Without Braces β
switch(day)
case 1:
System.out.println("Monday");
β This is INVALID in Java
π Curly braces {} are mandatory
π‘ Scenario-Based Programs
π½οΈ Scenario 1: Restaurant Menu Selection
`β Question
Select food using number:
1 β Idli
2 β Dosa
3 β Pongal
4 β Poori
`
private void scenario1() {
int item = 3;
switch (item) {
case 1:
System.out.println("Idli");
break;
case 2:
System.out.println("Dosa");
break;
case 3:
System.out.println("Pongal");
break;
case 4:
System.out.println("Poori");
break;
default:
System.out.println("Invalid Choice");
}
}
π€ Output
Pongal
π
Scenario 2: Day of the Week System
`β Question
1 β Monday
2 β Tuesday
β¦
7 β Sunday`
private void scenario2() {
int day = 1;
switch (day) {
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thursday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("Invalid Day");
}
}
π€ Output
Sunday
π± Scenario 3: Mobile Recharge Plan
`β Question
199 β 1GB/day
399 β 2GB/day
599 β 3GB/day`
private void scenario3() {
int plan = 399;
switch (plan) {
case 199:
System.out.println("1GB/day");
break;
case 399:
System.out.println("2GB/day");
break;
case 599:
System.out.println("3GB/day");
break;
default:
System.out.println("Plan not available");
}
}
π€ Output
2GB/day
π§βπΌ Scenario 4: Employee Role Access
`β Question
ADMIN β Full access
MANAGER β Limited access
EMPLOYEE β Basic access`
private void scenario4() {
String role = "ADMIN";
switch (role) {
case "ADMIN":
System.out.println("Full Access");
break;
case "MANAGER":
System.out.println("Limited Access");
break;
case "EMPLOYEE":
System.out.println("Basic Access");
break;
default:
System.out.println("No Access");
}
}
π€ Output
Full Access
π¦ Scenario 5: Traffic Fine System
`β Question
1 β No helmet
2 β No seatbelt
3 β Signal jump
4 β Over speed`
private void scenario5() {
int code = 2;
switch (code) {
case 1:
System.out.println("No Helmet Fine");
break;
case 2:
System.out.println("No Seatbelt Fine");
break;
case 3:
System.out.println("Signal Jump Fine");
break;
case 4:
System.out.println("Over Speed Fine");
break;
default:
System.out.println("Invalid Violation");
}
}
π€ Output
No Seatbelt Fine
β οΈ Important Concept: Fall Through
int x = 1;
switch(x) {
case 1:
System.out.println("A");
case 2:
System.out.println("B");
}
π€ Output
A
B
π Because break is missing
π§ Key Takeaways
β Switch is cleaner than multiple if-else
β break prevents unwanted execution
β default works like else
β Works with int, char, String
β Curly braces {} are mandatory
β continue is not directly used in switch
π Conclusion
Today I understood how to use switch-case effectively in Java.
This is very useful in automation for:
β Menu selection
β Handling multiple conditions
β Writing clean and readable code
Step by step, Iβm improving my Java logic for automation testing π
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)