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
}
β 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++;
}
π€ Output
1 1 1 1 1
β 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++;
}
π€ Output
1 2 3 4 5
β 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++;
}
π€ Output
1 3 5 7 9
β 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++;
}
π€ Output
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
β 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++;
}
π€ Output
15
30
β 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++;
}
π€ Output
3
5
6
9
10
β 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++;
}
π€ 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
β 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)