Skip to content
IGCSE·Tuition
Computer Science · Lessons

Trace a condition-controlled loop

Some loops do not know how many times they will run, and that is exactly what makes them easy to get wrong.

On this page
  1. When does each loop check its condition?
  2. Worked example 1: a WHILE loop
  3. Worked example 2: a REPEAT loop
  4. The mistake to watch for
  5. Check yourself
  6. Where this leads next

A condition-controlled loop repeats until a condition says to stop. The count is not fixed in advance. Cambridge-style pseudocode has two forms: WHILE with ENDWHILE, and REPEAT with UNTIL.

This lesson follows tracing a count-controlled loop in repetition and arrays. The new idea is that the test, not a counter, decides when the loop ends.

When does each loop check its condition?

A WHILE loop tests first. If the test is false on the very first check, the body never runs.

A REPEAT loop runs the body first and tests afterwards. The body always runs at least once. The loop stops when the UNTIL condition is true, which is the opposite way round to WHILE.

For every trace, add a column for the test and write down true or false each time it is checked.

Worked example 1: a WHILE loop

Balance ← 100
Years ← 0
WHILE Balance < 150 DO
   Balance ← Balance + 20
   Years ← Years + 1
ENDWHILE
OUTPUT Years, Balance
Test: Balance < 150Balance afterYears after
100 < 150 true1201
120 < 150 true1402
140 < 150 true1603
160 < 150 falseloop ends

The output is 3, 160. The last test is false, so there is no fourth pass.

Worked example 2: a REPEAT loop

N ← 64
Steps ← 0
REPEAT
   N ← N DIV 2
   Steps ← Steps + 1
UNTIL N < 5
OUTPUT Steps, N

DIV is whole-number division.

PassN afterSteps afterTest: N < 5
1321false, repeat
2162false, repeat
383false, repeat
444true, stop

The output is 4, 4. If N started at 3, a WHILE loop with the same test written as WHILE N >= 5 would run zero times, but this REPEAT loop would still run once and change N to 1.

The mistake to watch for

A common slip is to read UNTIL as “keep going while”.

Mistaken answer: “UNTIL N < 5 means the loop continues while N is less than 5, so it stops straight away.”

The student treated the UNTIL condition like a WHILE condition.

The correction is to read UNTIL N < 5 as “stop as soon as N is less than 5”. At the start N is 64, which is not less than 5, so the loop carries on. Put the words “stop when” in your margin next to every UNTIL.

Check yourself

1. What are the final value of X and the number of passes?

X ← 5
Passes ← 0
WHILE X > 0 DO
   X ← X - 2
   Passes ← Passes + 1
ENDWHILE
Show answer

X goes 5, 3, 1, then -1. Test results: 5 > 0 true, 3 > 0 true, 1 > 0 true, -1 > 0 false. The loop ran 3 passes and X is -1.

2. What is Count at the end?

Count ← 10
REPEAT
   Count ← Count + 1
UNTIL Count >= 10
Show answer

The body runs once before any test, so Count becomes 11. Then 11 >= 10 is true and the loop stops. Count is 11.

3. Why does this loop never finish?

N ← 10
WHILE N <> 0 DO
   N ← N - 3
ENDWHILE
Show answer

N takes the values 10, 7, 4, 1, -2, -5 and so on. It steps over 0 and never equals it, so N <> 0 stays true forever. Using WHILE N > 0 would fix it.

Where this leads next

Loops go wrong in a predictable way at the edges, which is the subject of repairing an off-by-one error. To watch a condition change step by step, use the pseudocode trace trainer, which stops runaway loops at a safety limit.

A teacher in Computer Science tuition can give you several new loops to trace until the stopping test is the first thing you check.

Questions people ask

What is the difference between WHILE and REPEAT UNTIL?

WHILE checks the condition before each pass, so the body may run zero times. REPEAT UNTIL checks after each pass, so the body always runs at least once. WHILE continues while its condition is true. REPEAT stops when its condition becomes true.

Can a loop run forever?

Yes. If nothing in the body moves the variables towards the stopping condition, the loop never ends. Exam questions sometimes ask you to spot this. Look for the variable in the condition and check that the body really changes it in the right direction.

How do I know which loop to trace first?

Read the keyword. WHILE has its test before the body, REPEAT has its test after the body, at UNTIL. Mark where the test happens on your trace table, then start with the starting values before the first test.

Updated:

Your next step

If you can trace a loop with a fixed count but freeze when the condition decides, a teacher can build fresh examples with you until checking the condition at the right moment feels automatic.

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.

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

Parent or guardian? Enquire here

9,000+ students helped through our service