| Feature | FOR Loop | WHILE Loop | REPEAT...UNTIL |
|---|---|---|---|
| Control Type | Count-controlled | Pre-condition | Post-condition |
| Min. Iterations | 0 (if start > end) | 0 | 1 |
| Usage Scenario | Known number of steps | Unknown steps; check first | Unknown steps; run once first |
| Termination | Reaches end value | Condition becomes False | Condition becomes True |
Check the Bounds: Always verify if a loop runs or times; this is a common source of 'off-by-one' errors in exam questions.
Infinite Loop Prevention: Ensure that the loop body contains code that modifies the control variable or condition, otherwise the loop will never terminate.
Selection Criteria: If an exam question asks which loop to use for 'reading a file until the end', choose a WHILE loop because the file might be empty (0 iterations).
Trace Tables: When dry-running code, create a column for every variable that changes inside the loop to track its state at the end of every iteration.
Logic Inversion: Students often confuse the termination logic of WHILE (runs while true) with UNTIL (runs until true/while false).
Scope Errors: Variables declared inside a loop may not be accessible outside the loop depending on the programming language's scope rules.
Nested Complexity: In nested loops, the total number of iterations is the product of the outer and inner loop counts; miscalculating this leads to performance issues.