🔁 Control Structures

Iterative Constructs (Loops)

⏱ 3 hr3 topicsInteractive
🎯 By the end: You can write for, while and do-while loops, convert one into another, and use break and continue to control a loop.

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);
LoopTests conditionBody runs at least once?
for / whileBefore the body (entry)No
do-whileAfter the body (exit)Yes
Key points
  • 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.

VariationMeans
Finite loopRuns a fixed number of times, then stops
Infinite loopNever stops (the condition is always true) — usually a mistake
Delay loopAn 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)
A for loop can use multiple counters: for (int i=0, j=9; i<j; i++, j--) — two variables changing each pass.
Key points
  • 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:

breakcontinue
EffectExits the loop completelySkips 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
}
Key points
  • 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:

  1. Print the numbers 1 to 20 using a for loop.
  2. Print the multiplication table of a number entered by the user.
  3. Add the numbers 1 to 100 using a while loop and print the total.
  4. 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.