A loop that never ends has a condition that never becomes false. A loop that misses the last item is stopping one pass too early. Both are found quickest by tracing the loop by hand, one row per pass.
This page gives one worked example of each, a repair and a short checklist. Notation here is simple, readable pseudocode. Your paper may use a specific style, so check your syllabus for the route that applies.
What does a loop that never ends look like?
Read this algorithm. The intention is to count down to zero in steps of 3.
Num ← 10
WHILE Num <> 0
Num ← Num - 3
ENDWHILE
OUTPUT "Done"
Trace it, writing the condition result each time.
| Pass | Num at start | Num <> 0 ? | Num after |
|---|---|---|---|
| 1 | 10 | TRUE | 7 |
| 2 | 7 | TRUE | 4 |
| 3 | 4 | TRUE | 1 |
| 4 | 1 | TRUE | −2 |
| 5 | −2 | TRUE | −5 |
Num goes 10, 7, 4, 1, −2, −5 and never equals exactly 0. The test “not equal to 0” stays TRUE forever, so “Done” is never reached.
How do I fix it?
Change the condition so that it stops when the value has gone past the target, not only when it equals the target.
Num ← 10
WHILE Num > 0
Num ← Num - 3
ENDWHILE
OUTPUT Num
Trace: 10 → 7 → 4 → 1 → −2. After the fourth pass Num is −2, and −2 > 0 is FALSE, so the loop stops. The output is −2.
The habit to keep: when a value moves in steps, prefer a condition based on greater than or less than, unless you have proved the step lands exactly on the target.
What does a loop that misses the last item look like?
Here is an array of five scores, counted from index 1 to 5 in this example. The task is to add up all of them.
Scores = [12, 7, 15, 9, 10]
Total ← 0
FOR i ← 1 TO 4
Total ← Total + Scores[i]
NEXT i
OUTPUT Total
| i | Scores[i] | Total after |
|---|---|---|
| 1 | 12 | 12 |
| 2 | 7 | 19 |
| 3 | 15 | 34 |
| 4 | 9 | 43 |
The output is 43, but the real total is 12 + 7 + 15 + 9 + 10 = 53. The loop stopped at 4, so Scores[5] = 10 was never added. The difference, 53 − 43 = 10, is exactly the missing item, which is a useful clue when you check your own.
How do I fix the off-by-one?
Make the loop’s last value the last index of the array.
Total ← 0
FOR i ← 1 TO 5
Total ← Total + Scores[i]
NEXT i
OUTPUT Total
Trace: 12, 19, 34, 43, 53. Output 53. ✓
If the array length can change, use its length instead of a fixed 5, for example FOR i ← 1 TO Length, where Length holds the number of items.
Which mistake is which?
| Symptom | Likely cause | First check |
|---|---|---|
| Program never finishes | Condition never turns FALSE | Does the loop change the variable the condition uses? |
| Stops, but the answer is short by one item | Limit one too low, or < instead of <= | Write first and last index beside the loop |
| Crashes or errors at the end | Limit one too high, reading past the array | Is the last index in range? |
| Works for 5 items, fails for 1 or 0 | Loop assumes at least two items | Test the smallest case by hand |
A checklist for any loop
- Write the first and last value the loop should handle.
- Trace the first pass and the last pass in full.
- Ask what makes the condition false, and check that the loop moves toward it.
- Test with an empty list or one item.
- Check that the result is sensible, for example by adding the list in a different way.
The restricted pseudocode trace trainer lets you step through an original algorithm and see each branch and loop decision, where your route allows it. The safe Python reasoning sandbox shows a bounded run with a stop if a loop goes on too long.
Where to go next
For the full idea, read repetition and arrays. Boundary bugs have their own page: my program runs but fails a boundary case. Then try the mixed questions in original practice, and see the learning guide for the whole map.
When tracing is not enough
Some students trace carefully and still cannot see why a loop goes wrong, because the belief behind it, such as “the variable updates by itself”, is invisible on paper. Watching you reason aloud makes that belief easy to find. Online one-to-one Computer Science tuition starts with a paid one-hour trial at the assigned teacher’s confirmed rate, from RM80.