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:
- Find the variables that exist before the loop and write their starting values in the first row.
- Write one row for each pass. Put the loop variable first.
- Update each variable in the order the lines appear in the body.
- Write the output at the point where the
OUTPUTline 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.
| Count | Count * 2 | Total after the pass |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 4 | 6 |
| 3 | 6 | 12 |
| 4 | 8 | 20 |
| 5 | 10 | 30 |
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.