Back to Topics/Loops & Iteration/FOR Loop
Page 9.xLoops & Iteration

FOR Loop

Learn the count-controlled FOR loop — the CIE pseudocode keywords FOR, TO, NEXT, and STEP — how to trace its execution, count by any amount, and apply it to real problems like summing ranges and building multiplication tables.

6
Sections
6
Knowledge Checks
12
Questions
Loops
Category

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.

Example: A Simple FOR Loop
FOR I <- 1 TO 5
    OUTPUT "Hello, world!"
NEXT I

This 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.

9.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>
PartPurposeExample
FORStarts the loopFOR I <- 1 TO 5
counterINTEGER variable that tracks progressI, Counter, Index
<-Assignment arrow — sets the start valueI <- 1
TOMarks the end value (inclusive)TO 5
STEPOptional increment (default 1)STEP 2
NEXTEnds the body; advances counterNEXT 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.

Example: Properly Declared FOR Loop
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 ", Sum

Both 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.

9.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

  1. Set the counter to the start value.
  2. Check: is counter ≤ end? (Or ≥ end if STEP is negative.) If NO, exit the loop.
  3. Run the loop body.
  4. At the NEXT statement, add STEP (default 1) to the counter.
  5. Go back to step 2.

Let's trace a complete example:

Example: Trace This Loop
DECLARE I : INTEGER
FOR I <- 1 TO 3
    OUTPUT "Iteration ", I
NEXT I
OUTPUT "Done. I is now ", I
StepIActionOutput
11Check 1 ≤ 3? YES → run bodyIteration 1
22NEXT adds 1; check 2 ≤ 3? YES → run bodyIteration 2
33NEXT adds 1; check 3 ≤ 3? YES → run bodyIteration 3
44NEXT adds 1; check 4 ≤ 3? NO → exit loop(none)
54Run statement after NEXTDone. 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.

9.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 I

Output: 0, 2, 4, 6, 8, 10

Counting Down

FOR I <- 10 TO 1 STEP -1
    OUTPUT I
NEXT I

Output: 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.

Example: STEP in Action
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, 1

Warning: 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.

9.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 = ", Sum

Accumulates 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 I

Uses 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 I

STEP 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.

Worked Example: Average of 5 Inputs
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 ", Average

Here 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.

9.6 Key Points Summary

Here is a quick recap of everything we've covered on FOR loops:

  • FOR is count-controlledA FOR loop runs a known, fixed number of times. Use it when the iteration count is decided before the loop starts.
  • Syntax: FOR / TO / NEXTEvery FOR loop needs FOR counter <- start TO end, a body, and NEXT counter. STEP is optional.
  • Declare the counterThe loop counter must be declared as an INTEGER before the loop, just like any other variable.
  • End value is inclusiveFOR I <- 1 TO 5 runs for I = 1, 2, 3, 4, AND 5 — five iterations, not four.
  • Counter is managed automaticallyDo NOT manually increment the counter inside the body. The NEXT statement handles it.
  • STEP changes the incrementDefault 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 endAfter "FOR I <- 1 TO 5" finishes, I holds 6. Exam questions often test this detail.

Question Bank

Read each question, think about your answer, then click "Reveal Answer" — 12 questions

Q1Structured

What is a FOR loop, and when should you use one?

Q2Structured

How many times does the body of "FOR I <- 1 TO 5" execute?

FOR I <- 1 TO 5
    // body
NEXT I
Q3Structured

Write the syntax of a basic FOR loop in CIE pseudocode (without STEP).

Q4Structured

What is the purpose of the NEXT keyword in a FOR loop?

Q5MC

What is the default STEP value in a FOR loop when STEP is omitted?

A0
B1
C-1
D10
Q6Structured

Trace the output of this loop line by line:

FOR I <- 1 TO 5
    OUTPUT I
NEXT I
Q7Structured

What does the STEP keyword do, and what kinds of values can it take?

Q8T/F

The loop counter variable must be declared with DECLARE before the FOR loop.

ATRUE
BFALSE
Q9Structured

Write a FOR loop that prints all even numbers from 2 to 10 (inclusive).

Q10Structured

After the loop "FOR I <- 1 TO 5" finishes, what is the value of I? Explain why.

FOR I <- 1 TO 5
    // body
NEXT I
// What is I here?
Q11T/F

The loop "FOR I <- 1 TO 1" executes the body exactly once.

ATRUE
BFALSE
Q12Structured

Write a FOR loop that counts down from 5 to 1, printing each number.

Previous: Operators
You've reached the end of the FOR Loop topic.
Next: REPEAT...UNTIL