9.1 What is a FOR Loop?
A FOR loop is a count-controlled loop — it repeats a block of code a specific, known number of times. You use a FOR loop whenever you know in advance exactly how many iterations you need (for example: "print the numbers 1 to 10" or "ask the user for 5 scores").
This makes the FOR loop different from the other two CIE loops:
FOR
Count-controlled. Runs a known number of times. The loop counter is managed automatically.
WHILE
Pre-test, condition-controlled. Checks the condition BEFORE each iteration; may run zero times.
REPEAT...UNTIL
Post-test, condition-controlled. Checks the condition AFTER each iteration; always runs at least once.
When to Choose FOR
Choose a FOR loop when the number of repetitions is fixed and known before the loop starts — for example, processing exactly 10 items, printing a multiplication table up to 12, or iterating over every element of an array by index. If the number of repetitions depends on a condition that changes during the loop, use WHILE or REPEAT...UNTIL instead.
FOR I <- 1 TO 5
OUTPUT "Hello, world!"
NEXT IThis loop prints "Hello, world!" exactly five times. The variable I is the loop counter — it automatically takes the values 1, 2, 3, 4, 5 in turn, and the loop body runs once for each value.
What is a FOR Loop?
5 items9.2 FOR Loop Syntax
The CIE pseudocode FOR loop has a strict three-line structure. Every FOR loop must have a matching NEXT statement — forgetting it is a common syntax error in exams.
General Form
FOR <counter> <- <start> TO <end> [STEP <increment>]
<body to repeat>
NEXT <counter>| Part | Purpose | Example |
|---|---|---|
| FOR | Starts the loop | FOR I <- 1 TO 5 |
| counter | INTEGER variable that tracks progress | I, Counter, Index |
| <- | Assignment arrow — sets the start value | I <- 1 |
| TO | Marks the end value (inclusive) | TO 5 |
| STEP | Optional increment (default 1) | STEP 2 |
| NEXT | Ends the body; advances counter | NEXT I |
Declaration is Required
The loop counter MUST be declared as an INTEGER before the loop, just like any other variable. The start and end values can be literals, variables, or expressions — but the counter itself must be a single INTEGER variable.
DECLARE I : INTEGER
DECLARE Sum : INTEGER
Sum <- 0
FOR I <- 1 TO 10
Sum <- Sum + I
NEXT I
OUTPUT "The sum of 1 to 10 is ", SumBoth I and Sum are declared at the top. The FOR loop runs from I = 1 to I = 10 inclusive — that is 10 iterations — adding I to Sum each time. The final output is 55.
FOR Loop Syntax
5 items9.3 How FOR Loops Work (Tracing)
To understand exactly what a FOR loop does, you need to trace it step by step. Tracing means following the value of the counter and any other variables through each iteration. This is a core exam skill — you will often be asked "what is the output?" or "what is the final value of X?".
Execution Flow of a FOR Loop
- Set the counter to the start value.
- Check: is counter ≤ end? (Or ≥ end if STEP is negative.) If NO, exit the loop.
- Run the loop body.
- At the
NEXTstatement, add STEP (default 1) to the counter. - Go back to step 2.
Let's trace a complete example:
DECLARE I : INTEGER
FOR I <- 1 TO 3
OUTPUT "Iteration ", I
NEXT I
OUTPUT "Done. I is now ", I| Step | I | Action | Output |
|---|---|---|---|
| 1 | 1 | Check 1 ≤ 3? YES → run body | Iteration 1 |
| 2 | 2 | NEXT adds 1; check 2 ≤ 3? YES → run body | Iteration 2 |
| 3 | 3 | NEXT adds 1; check 3 ≤ 3? YES → run body | Iteration 3 |
| 4 | 4 | NEXT adds 1; check 4 ≤ 3? NO → exit loop | (none) |
| 5 | 4 | Run statement after NEXT | Done. I is now 4 |
Watch the Final Counter Value
After FOR I <- 1 TO 3 finishes, I holds the value 4 — one past the end value. This is because the counter is incremented one final time before the condition check fails. Many exam questions test this exact detail.
Also note: if the start value is greater than the end value (with default STEP 1), the loop body runs zero times. For example, FOR I <- 5 TO 1 (without STEP -1) does nothing — the very first check 5 ≤ 1 is FALSE, so the body is skipped entirely.
Tracing FOR Loops
5 items9.4 Using STEP
The optional STEP keyword changes how much is added to the counter after each iteration. By default the STEP is 1 (the counter goes up by 1 each time). With STEP you can count by any integer amount — including negative values to count downwards.
Counting Up by 2
FOR I <- 0 TO 10 STEP 2
OUTPUT I
NEXT IOutput: 0, 2, 4, 6, 8, 10
Counting Down
FOR I <- 10 TO 1 STEP -1
OUTPUT I
NEXT IOutput: 10, 9, 8, ..., 1
How STEP Affects the Condition
When STEP is positive, the loop continues while counter ≤ end. When STEP is negative, the loop continues while counter ≥ end. The loop always moves the counter towards the end value and stops once it has passed it.
DECLARE I : INTEGER
// Count by fives
FOR I <- 5 TO 25 STEP 5
OUTPUT I
NEXT I
// Output: 5, 10, 15, 20, 25
// Countdown
FOR I <- 3 TO 1 STEP -1
OUTPUT I
NEXT I
// Output: 3, 2, 1Warning: STEP 0 is an Infinite Loop
Never use STEP 0 — the counter never changes, so the loop condition is always true and the loop runs forever. Also be careful with mismatched directions (e.g. FOR I <- 1 TO 10 STEP -1) — the loop body will run zero times because the counter moves away from the end value.
Using STEP
5 items9.5 Practical Examples
Let's look at four practical FOR loop patterns that come up frequently in exams and real programs: summing a range, counting down, building a multiplication table, and iterating with a custom STEP.
1. Sum of 1 to N
DECLARE I, N, Sum : INTEGER
INPUT N
Sum <- 0
FOR I <- 1 TO N
Sum <- Sum + I
NEXT I
OUTPUT "Sum = ", SumAccumulates the running total inside the loop.
2. Countdown
DECLARE I : INTEGER
FOR I <- 5 TO 1 STEP -1
OUTPUT I
NEXT I
OUTPUT "Lift off!"STEP -1 makes the counter decrease.
3. Times Table
DECLARE I, Num : INTEGER
INPUT Num
FOR I <- 1 TO 12
OUTPUT Num, " x ", I, " = ", Num * I
NEXT IUses the counter both as a multiplier and in the output.
4. Even Numbers
DECLARE I : INTEGER
FOR I <- 2 TO 20 STEP 2
OUTPUT I
NEXT ISTEP 2 visits only even values.
Pattern: The Accumulator
Examples 1 and 3 both use the accumulator pattern: a variable (like Sum) is initialised before the loop and updated inside the body on each iteration. This is the most common FOR loop pattern in CIE exams — recognise it instantly.
DECLARE I : INTEGER
DECLARE Score : INTEGER
DECLARE Total : INTEGER
DECLARE Average : REAL
Total <- 0
FOR I <- 1 TO 5
OUTPUT "Enter score ", I, ": "
INPUT Score
Total <- Total + Score
NEXT I
Average <- Total / 5
OUTPUT "The average score is ", AverageHere the loop runs exactly 5 times, prompting for a score each iteration and adding it to Total. After the loop, the average is computed by dividing by 5. Note the use of / (not DIV) so the result is a REAL.
Practical Examples
5 items9.6 Key Points Summary
Here is a quick recap of everything we've covered on FOR loops:
- FOR is count-controlled — A FOR loop runs a known, fixed number of times. Use it when the iteration count is decided before the loop starts.
- Syntax: FOR / TO / NEXT — Every FOR loop needs FOR counter <- start TO end, a body, and NEXT counter. STEP is optional.
- Declare the counter — The loop counter must be declared as an INTEGER before the loop, just like any other variable.
- End value is inclusive — FOR I <- 1 TO 5 runs for I = 1, 2, 3, 4, AND 5 — five iterations, not four.
- Counter is managed automatically — Do NOT manually increment the counter inside the body. The NEXT statement handles it.
- STEP changes the increment — Default STEP is 1. Use STEP 2 to count by twos, STEP -1 to count down. Never use STEP 0 (infinite loop).
- Final counter is one past the end — After "FOR I <- 1 TO 5" finishes, I holds 6. Exam questions often test this detail.