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.

SOCIAL SHARE CARD GENERATOR