DEV Community

Cover image for πŸš€ Day 22 of My Automation Journey – Switch Case Statement in Java
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 22 of My Automation Journey – Switch Case Statement in Java

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
}
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ 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");
Enter fullscreen mode Exit fullscreen mode

❌ 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Pongal
Enter fullscreen mode Exit fullscreen mode

πŸ“… 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Sunday
Enter fullscreen mode Exit fullscreen mode

πŸ“± 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

2GB/day
Enter fullscreen mode Exit fullscreen mode

πŸ§‘β€πŸ’Ό 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

Full Access
Enter fullscreen mode Exit fullscreen mode

🚦 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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

No Seatbelt Fine
Enter fullscreen mode Exit fullscreen mode

⚠️ Important Concept: Fall Through

int x = 1;

switch(x) {
    case 1:
        System.out.println("A");
    case 2:
        System.out.println("B");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

A
B
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ 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)