A loop repeats a block of code so you don't write it many times — print 1 to 100 with three lines, not a hundred. Loops are also called iterative constructs (ICSE Class 9).
1for, while and do-while
Java has three loops. Two are entry-controlled (test the condition before the body) and one is exit-controlled (test after):
// for — when you know how many times
for (int i = 1; i <= 5; i++)
System.out.println(i);
// while — entry-controlled
int j = 1;
while (j <= 5) { System.out.println(j); j++; }
// do-while — exit-controlled (runs at least once)
int k = 1;
do { System.out.println(k); k++; } while (k <= 5);| Loop | Tests condition | Body runs at least once? |
|---|---|---|
| for / while | Before the body (entry) | No |
| do-while | After the body (exit) | Yes |
- for, while are entry-controlled (test before the body); do-while is exit-controlled (test after).
- do-while always runs its body at least once; for/while may run zero times.
- Use for when you know the number of repetitions; while/do-while when you don't.
2Loop variations & conversion
The three loops can usually do the same job — a for can be rewritten as a while and vice-versa (loop inter-conversion). All loops need three things: initialisation, a condition, and an update.
| Variation | Means |
|---|---|
| Finite loop | Runs a fixed number of times, then stops |
| Infinite loop | Never stops (the condition is always true) — usually a mistake |
| Delay loop | An empty loop that just wastes time to create a pause |
for ( ; ; ) // infinite loop (no condition) for (int i=0; i<1000; i++); // delay loop (empty body)
for loop can use multiple counters: for (int i=0, j=9; i<j; i++, j--) — two variables changing each pass.- Every loop needs initialisation, a condition and an update; for/while/do-while can be converted into one another.
- Finite loops stop; infinite loops never stop (usually a bug); delay loops just waste time to pause.
- A for loop can manage multiple counters at once.
3break and continue
Two jump statements change a loop's flow:
| break | continue | |
|---|---|---|
| Effect | Exits the loop completely | Skips the rest of this pass, goes to the next |
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // stop the whole loop at 5
System.out.println(i); // prints 1 2 3 4
}
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // skip 3
System.out.println(i); // prints 1 2 4 5
}
- break exits the loop completely.
- continue skips the rest of the current pass and moves to the next iteration.
- Use them inside an if to react to a special condition.
★ Practical: loop it
In BlueJ, write programs that:
- Print the numbers 1 to 20 using a for loop.
- Print the multiplication table of a number entered by the user.
- Add the numbers 1 to 100 using a while loop and print the total.
- Print 1 to 10 but skip 7 (continue), and stop early at... use break to stop at the first multiple of 6.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.