Java Loops 🔄
Java Loops is a core Java concept covering master Java Loops. Learn how to automate repetition using For, While, This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.
Mentor's Note: Computers are the best employees in the world because they never get tired of doing the same thing over and over. A
loopis how you tell the computer to Repeat a task 100, 1000, or a million times! 💡
🌟 The Scenario: The School Bell & The Laundry 🧺
Imagine two different types of repetitive tasks.
- The For Loop (The School Bell): A school bell rings exactly 8 times a day. 🔔 You know the count before you even start.
- The While Loop (The Laundry Basket): You keep washing clothes WHILE the basket is not empty. 🧺 You don't know if there are 5 shirts or 50!
- The Do/While Loop (The First Plate): You wash at least ONE plate, then look at the pile to see if you should continue. 🍽️
- The Result: Total automation of boring, repetitive tasks. ✅
🎨 Visual Logic: The Loop Decision
| Loop Type | When to Use? | Key Feature |
|---|---|---|
for | You know the count 🔔 | Compact (1 line setup) |
while | You wait for a condition 🧺 | Check condition BEFORE running |
do-while | Run at least once 🍽️ | Check condition AFTER running |
for-each | For every item in a list 📦 | Simplest for Arrays |
📖 Concept Explanation
1. The for Loop 🔢
Used when you know exactly how many times to repeat.
- Syntax:
for (start; condition; update) - Logic: "Start at 1, go to 10, add 1 each time."
2. The while Loop 🧺
Used when you don't know the exact count, but you know the condition.
- Warning: If the condition is always true, you get an Infinite Loop! ♾️
3. The do-while Loop 🍽️
Almost same as while, but it runs the code block once before checking the condition.
4. The for-each Loop 📦
The easiest way to go through an Array.
- "For every
nameinnames, print it."
💻 Implementation: The Automation Lab
- Java (JDK 17+)
// 🛒 Scenario: School Bell & Laundry
// 🚀 Action: Using for and while loops
public class Main {
public static void main(String[] args) {
// 🔔 1. For Loop: Ring the bell 8 times
System.out.println("--- School Bell ---");
for (int i = 1; i <= 8; i++) {
System.out.println("Ring #" + i + " 🔔");
}
// 🧺 2. While Loop: Wash till empty
System.out.println("\n--- Laundry Day ---");
int clothesInBasket = 5;
while (clothesInBasket > 0) {
System.out.println("Washing one shirt... 🧺");
clothesInBasket--; // Important: Decrement or loop never ends!
}
// 📦 3. For-Each Loop: List the students
String[] students = {"Aryan", "Diya", "Kabir"};
for (String s : students) {
System.out.println("Student: " + s);
}
}
}
📊 Sample Dry Run (For Loop)
| Turn | Counter (i) | Check (i <= 3) | Output | New Counter |
|---|---|---|---|---|
| 1 | 1 | true ✅ | "Ring #1" | 2 ⬆️ |
| 2 | 2 | true ✅ | "Ring #2" | 3 ⬆️ |
| 3 | 3 | true ✅ | "Ring #3" | 4 ⬆️ |
| 4 | 4 | false ❌ | EXIT LOOP 🚪 | - |
📈 Technical Analysis: The Infinite Loop ♾️
An infinite loop happens when your condition never becomes false.
- Example:
while (true)or forgetting thei++in a for loop. - The Result: Your program hangs or crashes your computer! Always ensure your loop has an exit strategy. 🛡️
🎯 Practice Lab 🧪
Task: Use a for loop to print the multiplication table of 5 (5 x 1 = 5, 5 x 2 = 10, etc.).
Goal: Master the counter and loop multiplication. 💡
💡 Interview Tip 👔
"Interviewers often ask: 'How many times will a
do-whileloop run if the condition is false from the start?' Answer: Exactly once. It checks the condition only after the first run."
💡 Pro Tip: "Use
forwhen you count,whilewhen you wait. Choosing the right loop makes your code much easier to read!" - Vishnu Damwala