Loops in Java (for, while, do-while)
In this lesson: Learn Java loops with syntax, flowcharts, dry run and programs like tables and patterns — with output. In Hindi & English.
What is a loop
A loop repeats a block of code many times without you rewriting it. Java has three main loops: for (when you know the count), while (repeat until a condition changes), and do-while (run at least once). Loops are the heart of nearly every real program.
The for loop
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
Count: 2
Count: 3
Count: 4
Count: 5
A for loop has three parts: start (int i = 1), condition (i <= 5), and update (i++). It is ideal when you know exactly how many times to repeat.
Dry run of a for loop
| i | i <= 5? | then i++ | |
|---|---|---|---|
| 1 | yes | Count: 1 | 2 |
| 2 | yes | Count: 2 | 3 |
| 3 | yes | Count: 3 | 4 |
| 4 | yes | Count: 4 | 5 |
| 5 | yes | Count: 5 | 6 |
| 6 | no | condition false → loop ends | |
Each turn: check the condition, run the body if true, then do the update (i++). When the condition becomes false (i = 6), the loop stops.
The while loop
int i = 1;
while (i <= 3) {
System.out.println("i = " + i);
i++; // must update, or infinite loop
}
i = 2
i = 3
A while loop checks the condition first, then runs. Use it when you do not know the exact count. You must update the variable inside, or it never ends.
The do-while loop
int i = 10;
do {
System.out.println("Runs once: " + i);
i++;
} while (i <= 3); // condition is false, but body ran once
A do-while runs the body first, then checks the condition — so it always runs at least once, even when the condition is false from the start.
break and continue
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip 3
if (i == 5) break; // stop at 5
System.out.println(i);
}
2
4
continue skips the rest of this turn (so 3 is not printed), and break exits the loop entirely (so it stops before 5). They give fine control inside loops.
Common mistakes
- Forgetting to update the variable. A
whilewithouti++loops forever. - Off-by-one errors.
i < 5runs 4 times;i <= 5runs 5 — pick carefully. - Semicolon after for.
for(...);makes an empty loop — do not add;. - Using do-while when it must not run once. do-while always runs at least once.
Summary
- Loops repeat code without rewriting it.
foris best when you know the count.whilechecks first;do-whileruns once then checks.breakexits a loop;continueskips to the next turn.- Always update the loop variable to avoid infinite loops.
Next, make decisions with if-else in Java, or draw shapes in Floyd's triangle.
Frequently Asked Questions
What is a loop in Java?
for, while and do-while, each suited to a different situation.When should I use a for loop vs a while loop?
for loop when you know how many times to repeat (like 1 to 10). Use a while loop when you repeat until a condition changes and you do not know the count in advance.What is the difference between while and do-while?
while loop checks the condition before running, so it may run zero times. A do-while loop runs the body once first, then checks, so it always runs at least once.What do break and continue do?
break exits the loop immediately. continue skips the rest of the current turn and jumps to the next one. They give you finer control over how a loop behaves.