Skip to content
IGCSE·Tuition
Computer Science · Lesson

Repair a pseudocode loop termination condition

A loop that never stops, or stops one turn early, can hide in an algorithm that looks perfectly sensible.

On this page
  1. What are the usual termination bugs?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

A loop ends only when its condition changes in the right way. To repair one, trace the variable in the condition and ask whether it can ever make the loop stop at the intended point.

This lesson is part of pseudocode for the applicable syllabus. It uses the tracing skill from assignment and comparison and the loop forms covered in repetition and arrays.

What are the usual termination bugs?

There are four. First, the condition variable is never updated. Second, the update moves it the wrong way.

Third, the test uses <> or = and the value jumps over it. Fourth, the test is off by one, using < where <= is needed.

For each bug, the fix is the same: decide what the loop should do, then make the update and the test agree.

Worked example

This loop should add 1, 3, 5, 7 and 9, and then stop.

Total ← 0
Count ← 1
WHILE Count <> 10 DO
   Total ← Total + Count
   Count ← Count + 2
ENDWHILE
OUTPUT Total

Trace the first rounds:

RoundCount at testCount <> 10Total after bodyCount after body
11TRUE13
23TRUE45
35TRUE97
47TRUE169
59TRUE2511
611TRUE3613

Count takes only odd values, so it jumps over 10 and the test is TRUE forever. The loop never terminates.

Repair: change the test to WHILE Count < 10 DO. Now round 5 ends with Count = 11, the test 11 < 10 is FALSE, and the loop stops. Total is 25. Check: 1 + 3 + 5 + 7 + 9 = 25.

The mistake to watch for

A common slip is fixing the symptom, not the cause.

Mistaken repair: change Count + 2 to Count + 1 so that Count reaches 10.

The loop now stops, but it adds 1 + 2 + 3 + … + 9 = 45. The algorithm no longer does what it was meant to do.

The correction is to keep the intended behaviour and change the test. Use < or <= rather than <> when a variable steps by more than 1, because a “not equal” test is only safe if the value will certainly land on the target.

Check yourself

1. X ← 20, WHILE X > 0 DO, X ← X - 6, ENDWHILE. What is X at the end, and how many times does the loop run?

Show answer

X goes 20, 14, 8, 2, then -4. The loop body runs 4 times and X ends at -4.

2. Why does Flag ← TRUE, WHILE Flag = TRUE DO OUTPUT "Hi" ENDWHILE never end?

Show answer

Nothing inside the loop changes Flag, so the condition stays TRUE. Add an update such as Flag ← FALSE under the right condition.

3. Start with Count ← 1, then REPEAT Count ← Count + 1 UNTIL Count > 5. What is Count at the end?

Show answer

Count goes 2, 3, 4, 5, 6. It stops when Count is 6, so Count is 6.

Where this leads next

Finish the module with checking the exam year before choosing notation. The restricted pseudocode trace trainer stops runaway loops at a safety limit, so you can test a faulty loop without freezing anything.

Debugging is a skill that grows from seeing your own mistakes explained. That is something our teachers do in online one-to-one Computer Science tuition.

Questions people ask

What is a termination condition?

It is the test that decides when a loop stops. For a WHILE loop, the loop runs while the condition is TRUE. For a REPEAT loop, it runs until the condition becomes TRUE. A bug appears when the variables in the test can never reach the stopping value.

What is the difference between WHILE and REPEAT for termination?

A WHILE loop tests first, so it may run zero times. A REPEAT loop tests last, so it always runs at least once. The WHILE condition describes when to continue; the REPEAT condition describes when to stop.

How do I find an infinite loop in a trace?

Trace four or five rounds and look at the variable in the condition. If it moves away from the stopping value, or jumps over it, or never changes, the loop will not end. Then change the update or the test.

Updated:

Your next step

If you can run a loop by hand but cannot yet spot why a given one fails, a one-to-one teacher can give you broken loops to repair until the pattern is familiar.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students