DEV Community

Cover image for πŸš€ Day 20 of My Automation Journey – Control Flow Statements (if, else if, else).
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 20 of My Automation Journey – Control Flow Statements (if, else if, else).

Today I explored one of the most important concepts in Java – Control Flow Statements.

Control flow statements help us decide:

πŸ‘‰ Which code should execute
πŸ‘‰ When it should execute
πŸ‘‰ How many times it should execute

These are very important when building real-world applications and automation scripts.

πŸ”Ή What are Control Flow Statements?

Control flow statements control the execution flow of a program.
**
There are 3 types:**

1️⃣ Decision Making Statements
2️⃣ Looping Statements
3️⃣ Jump Statements

πŸ” 1️⃣ Decision Making Statements

Used to take decisions based on conditions.

βœ… if

Executes code only if condition is true

βœ… else-if

Used to check multiple conditions

βœ… else

Executes when all conditions fail

βœ… switch

Used for multiple fixed values

πŸ” 2️⃣ Looping Statements

βœ” while loop
βœ” for loop
βœ” do-while loop

πŸ”„ 3️⃣ Jump Statements

| Statement  | Purpose         |
| ---------- | --------------- |
| `break`    | Stops loop      |
| `continue` | Skips iteration |
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Scenario-Based Questions (if / else-if / else)
🏦 Scenario 1: ATM Withdrawal System
❓ Question

A user tries to withdraw money from an ATM.
If withdrawal amount is less than or equal to balance β†’ show "Successfully debited"
Else β†’ show "Insufficient balance"

private void scenario1() {

    int balance = 1000;
    int userWA = 200;

    if (userWA <= balance) {
        System.out.println("Successfully Debited " + userWA);
    } else {
        System.out.println("Insufficient Balance");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

βœ” Condition checks whether user has enough balance
βœ” Executes appropriate block based on result

πŸ“€ Output

Successfully Debited 200
Enter fullscreen mode Exit fullscreen mode

πŸ” Scenario 2: Online Food Delivery Status
❓ Question

An order goes through multiple stages:
If "Placed" β†’ show Waiting for confirmation
If "Preparing" β†’ show Food is being prepared
If "Out for delivery" β†’ show Delivery partner on the way
Else β†’ show Delivered

private void scenario2(String userState) {

    if (userState.equalsIgnoreCase("placed")) {
        System.out.println("Waiting for restaurant confirmation");
    } else if (userState.equalsIgnoreCase("preparing")) {
        System.out.println("Food is being prepared");
    } else if (userState.equalsIgnoreCase("out for delivery")) {
        System.out.println("Delivery partner on the way");
    } else {
        System.out.println("Delivered");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

βœ” Uses else-if ladder for multiple conditions
βœ” equalsIgnoreCase() handles case sensitivity

πŸŽ“ Scenario 3: Exam Result System
❓ Question

A student’s result is based on marks:
< 35 β†’ Fail
35–60 β†’ Pass
60–80 β†’ First Class
80 β†’ Distinction

private void scenario3() {

    int mark = 81;

    if (mark < 35)
        System.out.println("Fail");
    else if (mark >= 35 && mark <= 60)
        System.out.println("Pass");
    else if (mark > 60 && mark <= 80)
        System.out.println("First Class");
    else
        System.out.println("Distinction");
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

βœ” Uses range conditions with && operator
βœ” Ensures only one block executes

πŸ“€ Output

Distinction
Enter fullscreen mode Exit fullscreen mode

🚦 Scenario 4: Traffic Signal Control
❓ Question

A driver reacts based on signal color:
Red β†’ Stop
Yellow β†’ Get ready
Green β†’ Go
Else β†’ Invalid signal

private void scenario4(String signalcolor) {

    if (signalcolor.equalsIgnoreCase("red"))
        System.out.println("Stop the Vehicle");
    else if (signalcolor.equalsIgnoreCase("yellow"))
        System.out.println("Get Ready");
    else if (signalcolor.equalsIgnoreCase("green"))
        System.out.println("Go");
    else
        System.out.println("Invalid Signal");
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

βœ” Uses string comparison
βœ” Handles different input cases using equalsIgnoreCase()

πŸ” Scenario 5: Login Authentication System
❓ Question

A user tries to log in:

If username is wrong β†’ "Invalid Username"
Else if password is wrong β†’ "Invalid Password"
Else if account is locked β†’ "Account Locked"
Else β†’ Allow login
static String username = "bala";
static String password = "bala1234";
static boolean accountLocked = true;

public class Decisionmaking {

    static String username = "bala";
    static String password = "bala1234";
    static boolean accountLocked = true;
    public static void main(String[] args) {
        Decisionmaking demo = new Decisionmaking();
        demo.sceanrio5();
private void scenario5() {

    if (!username.equals("bala"))
        System.out.println("Invalid Username");
    else if (!password.equals("bala1234"))
        System.out.println("Invalid Password");
    else if (accountLocked)
        System.out.println("Account is Locked");
    else
        System.out.println("Allow login");
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

βœ” ! operatoris used for negation
βœ” Conditions are evaluated in sequence
βœ” Once matched β†’ remaining conditions are skipped

πŸ“€ Output

Account is Locked
Enter fullscreen mode Exit fullscreen mode

🧠 Key Takeaways

βœ” Control flow defines how program executes
βœ” if-else handles decision making
βœ” else-if handles multiple conditions
βœ” Loops reduce repetitive code
βœ” break & continue control loops

🏁 Conclusion

Today’s learning helped me understand how Java programs make decisions and control execution flow.

These concepts are extremely important in automation testing, where we need to:

βœ” Handle different test conditions
βœ” Validate application behavior
βœ” Write dynamic scripts

Step by step, I’m building a strong foundation in Java for Selenium Automation πŸš€

πŸ€– 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)