Skip to content
IGCSE·Tuition

Computer Science · Help with common difficulties

My loop runs forever or misses the last item

The loop looks right when you read it, and yet it either never ends or quietly leaves out the last value.

On this page
  1. What does a loop that never ends look like?
  2. How do I fix it?
  3. What does a loop that misses the last item look like?
  4. How do I fix the off-by-one?
  5. Which mistake is which?
  6. A checklist for any loop
  7. Where to go next
  8. When tracing is not enough

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.

PassNum at startNum <> 0 ?Num after
110TRUE7
27TRUE4
34TRUE1
41TRUE−2
5−2TRUE−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
iScores[i]Total after
11212
2719
31534
4943

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?

SymptomLikely causeFirst check
Program never finishesCondition never turns FALSEDoes the loop change the variable the condition uses?
Stops, but the answer is short by one itemLimit one too low, or < instead of <=Write first and last index beside the loop
Crashes or errors at the endLimit one too high, reading past the arrayIs the last index in range?
Works for 5 items, fails for 1 or 0Loop assumes at least two itemsTest the smallest case by hand

A checklist for any loop

  1. Write the first and last value the loop should handle.
  2. Trace the first pass and the last pass in full.
  3. Ask what makes the condition false, and check that the loop moves toward it.
  4. Test with an empty list or one item.
  5. 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.

Questions people ask

Why does my WHILE loop never stop?

The condition never becomes false. Usually the variable in the condition is not changed inside the loop, or it changes in a way that skips the stopping value, such as counting down by 3 and waiting for exactly 0. Trace three passes by hand and look at what the condition sees each time.

Why does my loop skip the last item?

This is an off-by-one error. The loop stops one step early, for example because the limit is one too low or the test uses < where <= is needed. Write the first and last index by hand, then check that your loop visits both.

Do arrays start at 0 or 1?

It depends on the notation and language your exam route uses. Some pseudocode examples count from 1 and Python counts from 0. Check the Cambridge syllabus for your code and year, then write the first and last index on your working before writing the loop.

How can I test a loop without running it?

Use a trace table. Make one column per variable and one row per pass, and write the condition result beside each row. If a row never changes the value the condition depends on, you have found the forever loop before running anything.

Sources

  1. Cambridge IGCSE Computer Science 0478 syllabus page

Updated:

Your next step

If loop errors keep returning after you have fixed them once, a teacher working one to one can watch you trace and spot the habit behind them.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

9,000+ students helped through our service