Skip to content
IGCSE·Tuition
Computer Science · Lessons

Trace a count-controlled loop

A FOR loop looks harmless until you have to say exactly what it printed on the third pass.

On this page
  1. How does a FOR loop work?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

A count-controlled loop repeats its body a fixed number of times, decided before the loop starts. In Cambridge-style pseudocode it is the FOR loop that ends with NEXT. You are usually asked to trace it, which means writing down what every variable holds on every pass.

This lesson opens repetition and arrays. The skill returns in totals, searches and every array question that follows.

How does a FOR loop work?

The line FOR Count ← 1 TO 5 does three things. It sets Count to 1, runs the body, then adds 1 to Count and runs the body again. It stops after the pass where Count is 5, because the end value is included.

Use this routine for every trace:

  1. Find the variables that exist before the loop and write their starting values in the first row.
  2. Write one row for each pass. Put the loop variable first.
  3. Update each variable in the order the lines appear in the body.
  4. Write the output at the point where the OUTPUT line runs, not at the end.

Worked example

Trace this algorithm.

Total ← 0
FOR Count ← 1 TO 5
   Total ← Total + Count * 2
NEXT Count
OUTPUT Total

Total starts at 0. On each pass we add Count * 2, and the multiplication happens before the addition.

CountCount * 2Total after the pass
122
246
3612
4820
51030

The loop ran five times and the output is 30. Check: 2 + 4 + 6 + 8 + 10 = 30.

A loop can also count down with a negative step.

FOR k ← 10 TO 1 STEP -3
   OUTPUT k
NEXT k

The values are 10, 7, 4 and 1. The next value would be -2, which is below the end value of 1, so the loop stops. It runs four times.

The mistake to watch for

A common slip is to think the end value is left out.

Mistaken answer: “FOR Count ← 1 TO 5 runs four times, so Total is 20.”

The student counted the gaps between 1 and 5 instead of the values 1, 2, 3, 4, 5.

The correction is to list the values of the loop variable one by one and count them. Another habit that helps is to write the rows of the trace table before you do any arithmetic. The table makes a missing row obvious.

Check yourself

1. How many times does FOR i ← 3 TO 7 run, and what is the last value of i?

Show answer

The values are 3, 4, 5, 6, 7, so it runs 5 times and the last value is 7. Check: 7 - 3 + 1 = 5.

2. What is output?

Result ← 1
FOR n ← 1 TO 4
   Result ← Result * n
NEXT n
OUTPUT Result
Show answer

Result is 1 at the start, then 1 × 1 = 1, 1 × 2 = 2, 2 × 3 = 6, 6 × 4 = 24. The output is 24.

3. A loop starts with Sum ← 0 and runs FOR j ← 2 TO 8 STEP 2 with Sum ← Sum + j. What is Sum at the end?

Show answer

The values of j are 2, 4, 6 and 8. The sum is 2 + 4 + 6 + 8 = 20.

Where this leads next

Next, look at a loop that stops because of a condition rather than a count in tracing a condition-controlled loop. Practise stepping through small loops in the pseudocode trace trainer or test the same idea in the Python reasoning sandbox.

If tracing is where your marks go, a teacher in online one-to-one Computer Science tuition can work through your own trace tables and find the line where they drift.

Questions people ask

How many times does FOR i ← 1 TO 5 run?

Five times. The loop variable takes the values 1, 2, 3, 4 and 5, and the end value is included. A quick check is end minus start plus one: 5 - 1 + 1 = 5. Counting only the gaps between the numbers is the usual reason students answer four.

What is the value of the loop variable after the loop ends?

Do not rely on it. In many languages it holds the first value past the end, but pseudocode questions normally do not ask for it. Trace the values inside the loop and only use the variable after NEXT if the question clearly says what it holds.

Do I need STEP in the exam?

STEP is part of Cambridge-style FOR loops, so be able to read it. Without STEP the loop counts up by one. A negative STEP counts down. Check the syllabus for your exam year on the Cambridge page for the exact notation expected.

Updated:

Your next step

If your trace tables are right for short loops but drift when a loop has two variables, a teacher can trace one of your own attempts with you and show which line the drift starts on.

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