← IGCSE Computer Science Paper 2

FOR ... TO ... NEXT loop (7a)

Repeats the code once for each integer from 1 to 30 inclusive, up to NEXT Index. Runs once if both values are equal and not at all if the start value is bigger.

Understanding FOR ... TO ... NEXT loop (7a) in IGCSE Computer Science Paper 2

What it does: NEXT Index marks the end of the loop body and uses the same identifier that was named in the FOR line. 10 is greater than 1, so there are no integers to count up through and the statements inside the loop are skipped. A FOR loop includes both the start value and the end value, so Count takes the values 1, 2, 3, 4 and 5. Cambridge pseudocode closes a FOR loop with NEXT followed by the loop variable. ENDFOR is not used. When value1 equals value2 there is exactly one integer in the range, so the loop body runs one time.

Common mistakes: A FOR loop in Cambridge pseudocode ends with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Index. A FOR loop uses the keyword TO between the start and end values. UNTIL belongs to REPEAT loops. It should be FOR Index ← 1 TO 10.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 37–46) and word search (Modules 27–46).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
How many times does the body of FOR Count ← 1 TO 5 run?
4 times5 times
Matches
FOR Index ← 1 TO 30 OUTPUT Index ENDFOR
↓
A FOR loop must end with NEXT Index, not ENDFOR. ENDFOR is not Cambridge pseudocode.
Applying
Meanings
Term
FOR Index ← 1 TO 30
Meaning
Repeats the code once for each integer from 1 to 30 inclusive, up to NEXT Index. Runs once if both values are equal and not at all if the start value is bigger.
Practice
FOR Index ← 1 TO 5
Testing
Gaps
FOR Index ← 1 30
Check Answer
Spots
3 OUTPUT Index
4ENDFOR

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Nebula Cat' badge — Orbit Debugger, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. How many times does the body of FOR Count ← 1 TO 5 run?
A. 5 times ✓
B. 4 times
A FOR loop includes both the start value and the end value, so Count takes the values 1, 2, 3, 4 and 5.
2. Which line correctly ends the loop that starts with FOR Index ← 1 TO 30?
A. ENDFOR Index
B. NEXT Index ✓
Cambridge pseudocode closes a FOR loop with NEXT followed by the loop variable. ENDFOR is not used.
3. How many times does FOR Num ← 4 TO 4 run?
A. Once ✓
B. Never
When value1 equals value2 there is exactly one integer in the range, so the loop body runs one time.
4. What happens when FOR Num ← 10 TO 1 is run?
A. The body does not run at all ✓
B. It counts down from 10 to 1
Without a negative STEP, a start value bigger than the end value means no values are left to count, so the loop is skipped.
5. Which data type must the loop variable in a FOR loop have?
A. STRING
B. INTEGER ✓
A FOR loop counts through whole numbers, so the loop variable must be an INTEGER.

Match the Pairs

FOR Index ← 1 TO 30 → Repeats the statements below once for each integer from 1 to 30 inclusive, ending at NEXT Index
FOR Count ← 5 TO 5 → Runs the loop body exactly once, because the start value and end value are equal
A FOR loop must be closed with NEXT followed by the same loop variable → FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
The loop does not run at all if the start value is bigger than the end value → FOR Index ← 10 TO 1
FOR Index ← 1 TO 30 OUTPUT Index ENDFOR → A FOR loop must end with NEXT Index, not ENDFOR. ENDFOR is not Cambridge pseudocode.
FOR Index ← 1 TO 30 OUTPUT Index NEXT Count → The NEXT line uses a different identifier (Count) from the loop variable (Index).

Key Term

FOR Index ← 1 TO 30
Repeats the code once for each integer from 1 to 30 inclusive, up to NEXT Index. Runs once if both values are equal and not at all if the start value is bigger.

Fill in the Blanks

1. FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Hint: Type the keyword that joins the start value and the end value of the loop.
2. FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Hint: Type the keyword that marks the end of the FOR loop body.
3. FOR Index ← 1 TO 30 OUTPUT Index NEXT Index
Hint: Type the assignment symbol that gives the loop variable its starting value.
4. DECLARE Index : INTEGER FOR Index ← 1 TO 30 NEXT Index
Hint: Type the data type that a FOR loop variable must have.
5. FOR Index ← 1 TO 1 OUTPUT "Hello" NEXT Index
Hint: Type the end value that makes this loop run exactly once because it equals the start value.

Spot the Error

Question 1
A DECLARE Index : INTEGER
B FOR Index ← 1 TO 3
C OUTPUT Index
D ENDFOR
Line D — A FOR loop in Cambridge pseudocode ends with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Index.
Question 2
A FOR Index ← 1 UNTIL 10
B OUTPUT Index * 2
C NEXT Index
Line A — A FOR loop uses the keyword TO between the start and end values. UNTIL belongs to REPEAT loops. It should be FOR Index ← 1 TO 10.
Question 3
A DECLARE Total : INTEGER
B Total ← 0
C FOR Index = 1 TO 4
D Total ← Total + Index
E NEXT Index
Line C — The loop variable is given its start value with the assignment arrow ←. The = sign is a comparison operator. It should be FOR Index ← 1 TO 4.
Question 4
A DECLARE Index : INTEGER
B OUTPUT "Start"
C FOR Index ← 1.5 TO 5
D OUTPUT Index
E NEXT Index
Line C — The loop variable must be an INTEGER, so the start value cannot be a decimal such as 1.5. It should be FOR Index ← 1 TO 5.
Question 5
A DECLARE Num : INTEGER
B FOR Num ← 1 TO 3
C OUTPUT Index * Index
D NEXT Num
Line C — The loop variable is Num, so the body must use Num. Index has not been declared or given a value. It should be OUTPUT Num * Num.

Student with a class code? Join your class →