DEV Community

Cover image for πŸš€ Day 24 of My Automation Journey – Looping Statements (While Loop)
bala d kaveri
bala d kaveri

Posted on

πŸš€ Day 24 of My Automation Journey – Looping Statements (While Loop)

Today I explored Looping Statements in Java, especially the while loop.

Loops are very important because they help us repeat a task multiple times without writing duplicate code.

πŸ”Ή What is a Loop?

πŸ‘‰ A loop is used to execute a block of code repeatedly based on a condition.

πŸ” Types of Loops in Java

βœ” While Loop
βœ” For Loop
βœ” Do-While Loop

πŸ‘‰ Today focus: While Loop

πŸ”Ή While Loop Syntax

while(condition) {
    // code
}
Enter fullscreen mode Exit fullscreen mode

βœ” Condition is checked first
βœ” If true β†’ executes
βœ” If false β†’ stops

πŸ” Scenario 1: Print same number multiple times
❓ Question

Print 1 five times

int i = 1;

while(i <= 5) {
    System.out.print(1 + " ");
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

1 1 1 1 1
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Loop runs 5 times
βœ” Always prints 1

πŸ” Scenario 2: Print numbers from 1 to 5
❓ Question

Print numbers from 1 to 5

int i = 1;

while(i <= 5) {
    System.out.print(i + " ");
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” i increases each time
βœ” Prints updated value

πŸ” Scenario 3: Print odd numbers
❓ Question

Print odd numbers from 1 to 10

int i = 1;

while(i <= 10) {
    if(i % 2 != 0) {
        System.out.print(i + " ");
    }
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

1 3 5 7 9
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” i % 2 != 0 β†’ odd numbers
βœ” Even numbers skipped

πŸ” Scenario 4: Multiplication Table (Γ—5)
❓ Question

Print multiplication table of 5

int i = 1;

while(i <= 5) {
    System.out.println(i + " * 5 = " + i * 5);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Loop runs 5 times
βœ” Multiplies i * 5

πŸ” Scenario 5: Multiples of 3 AND 5
❓ Question

Print numbers divisible by both 3 and 5

int i = 1;

while (i <= 30) {
    if (i % 3 == 0 && i % 5 == 0)
        System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

15
30
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” && β†’ both conditions must be true
βœ” Only multiples of 15 printed

πŸ” Scenario 6: Multiples of 3 OR 5
❓ Question

Print numbers divisible by 3 OR 5

int i = 1;

while (i <= 10) {
    if (i % 3 == 0 || i % 5 == 0)
        System.out.println(i);
    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output

3
5
6
9
10
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” || β†’ any one condition true
βœ” More numbers printed

πŸ” Scenario 7: Multiple Conditions with if-else
❓ Question

Print different messages based on divisibility

int i = 1;

while (i <= 20) {

    if (i % 3 == 0 && i % 5 == 0)
        System.out.println(i + " multiply by both");

    else if (i % 3 == 0)
        System.out.println(i + " multiply by 3");

    else if (i % 5 == 0)
        System.out.println(i + " multiply by 5");

    else
        System.out.println(i);

    i++;
}
Enter fullscreen mode Exit fullscreen mode

πŸ“€ Output (Sample)

8
9 multiply by 3
10 multiply by 5
11
12 multiply by 3
13
14
15 multiply by both
16
17
18 multiply by 3
Enter fullscreen mode Exit fullscreen mode

βœ… Explanation

βœ” Priority matters
βœ” First matching condition executes

⚠️ Common Mistakes

❌ Forgetting i++ β†’ Infinite loop 😱
❌ Wrong condition β†’ loop never runs
❌ Using = instead of ==

🧠 Key Takeaways

βœ” While loop checks condition first
βœ” Used when number of iterations is unknown
βœ” i++ is very important
βœ” && β†’ both conditions
βœ” || β†’ any one condition
βœ” Can combine with if-else for complex logic

🏁 Conclusion

Today I learned how to use while loop effectively with conditions.

This is very useful in automation for:

βœ” Repeating test steps
βœ” Data-driven testing
βœ” Iterating through elements

Step by step, I’m improving my Java logic 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)