← IGCSE Computer Science Paper 2

How many times a FOR loop runs

The number of times a FOR loop runs. FOR i ← 1 TO 5 runs 5 times, 3 TO 3 runs once, and 5 TO 1 runs zero times because the start value is bigger than the end value.

Understanding How many times a FOR loop runs in IGCSE Computer Science Paper 2

What it does: 3 - 3 + 1 = 1, so the body runs one time with i equal to 3 and then the loop finishes. The loop runs 5 - 1 + 1 = 5 times. i takes the values 1, 2, 3, 4 and 5. You must add 1 because both the start value and the end value are used. Forgetting the + 1 is a classic off-by-one mistake. 1 TO 10 runs 10 - 1 + 1 = 10 times. 1 TO 9 would run only 9 times, which is an off-by-one error. 7 - 2 + 1 = 6. The values of i are 2, 3, 4, 5, 6 and 7. Subtracting without adding 1 gives the wrong answer of 5.

Common mistakes: Assignment uses the arrow ←, not =. The line should be Count ← Count + 1, so Count goes up by 1 on each of the 5 runs of the loop. The loop should run exactly once, which happens when the start and end values are equal. It should be FOR i ← 3 TO 3. With 3 TO 2 the start is bigger than the end, so the loop runs zero times.

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 166–175) and word search (Modules 166–187).

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 FOR i ← 1 TO 5 run its loop body?
5 times4 times
Matches
FOR i ← 1 TO 10 // meant to run 9 times
↓
Off-by-one error: 1 TO 10 runs 10 times, not 9. The count was worked out as end - start and the + 1 was forgotten.
Applying
Meanings
Term
end - start + 1
Meaning
The number of times a FOR loop runs. FOR i ← 1 TO 5 runs 5 times, 3 TO 3 runs once, and 5 TO 1 runs zero times because the start value is bigger than the end value.
Practice
DECLARE Count : INTEGER
Testing
Gaps
FOR i ← 1 TO
Check Answer
Spots
2FOR i ← 1 TO 5
3 Count = Count + 1

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

The 'Hermit Crab' badge — Definition Collector, 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 FOR i ← 1 TO 5 run its loop body?
A. 5 times ✓
B. 4 times
The loop runs 5 - 1 + 1 = 5 times. i takes the values 1, 2, 3, 4 and 5.
2. Which formula gives the number of times a FOR loop runs?
A. end - start
B. end - start + 1 ✓
You must add 1 because both the start value and the end value are used. Forgetting the + 1 is a classic off-by-one mistake.
3. FOR i ← 3 TO 3 ... NEXT i. What happens?
A. The loop body never runs
B. The loop body runs exactly once ✓
3 - 3 + 1 = 1. When the start and end values are equal the loop runs once, with i equal to 3.
4. FOR i ← 5 TO 1 ... NEXT i. What happens?
A. The loop body runs zero times ✓
B. The loop body runs 5 times, counting down
A FOR loop without STEP counts up. Because the start value 5 is bigger than the end value 1, the loop is skipped completely.
5. A student wants a loop to run 10 times. Which loop header is correct?
A. FOR i ← 1 TO 10 ✓
B. FOR i ← 1 TO 9
1 TO 10 runs 10 - 1 + 1 = 10 times. 1 TO 9 would run only 9 times, which is an off-by-one error.

Match the Pairs

FOR i ← 1 TO 5 → The loop body runs 5 times, because 5 - 1 + 1 = 5
FOR i ← 5 TO 1 → The loop body runs zero times, because the start value is bigger than the end value
When the start and end values are equal, the loop runs exactly once → FOR i ← 3 TO 3
The number of repetitions is end - start + 1, so this loop runs 8 times → FOR Count ← 4 TO 11
FOR i ← 1 TO 10 // meant to run 9 times → Off-by-one error: 1 TO 10 runs 10 times, not 9. The count was worked out as end - start and the + 1 was forgotten.
FOR i ← 10 TO 1 // meant to run 10 times → The start value is bigger than the end value, so the loop runs zero times instead of 10.

Key Term

end - start + 1
The number of times a FOR loop runs. FOR i ← 1 TO 5 runs 5 times, 3 TO 3 runs once, and 5 TO 1 runs zero times because the start value is bigger than the end value.

Fill in the Blanks

1. FOR i ← 1 TO 5 OUTPUT i NEXT i
Hint: Type the end value that makes this loop run exactly five times.
2. FOR i ← 3 TO 3 OUTPUT i NEXT i
Hint: Type the end value that makes the loop run only once, because it equals the start value.
3. FOR i ← 5 TO 1 // runs 0 times OUTPUT i NEXT i
Hint: Type how many times this loop runs when the start value is bigger than the end value.
4. FOR i ← 3 TO 8 // runs 8 - 3 + 1 times OUTPUT i NEXT i
Hint: Type the number added in the formula end - start + something to count the runs.
5. FOR i ← 0 TO 9 // runs 10 times OUTPUT i NEXT i
Hint: Type the end value; starting at zero, it is one less than the number of runs (avoid the off-by-one mistake).

Spot the Error

Question 1
A Count ← 0
B FOR i ← 1 TO 5
C Count = Count + 1
D NEXT i
E OUTPUT Count
Line C — Assignment uses the arrow ←, not =. The line should be Count ← Count + 1, so Count goes up by 1 on each of the 5 runs of the loop.
Question 2
A // Runs once
B FOR i ← 3 TO 2
C OUTPUT i
D NEXT i
Line B — The loop should run exactly once, which happens when the start and end values are equal. It should be FOR i ← 3 TO 3. With 3 TO 2 the start is bigger than the end, so the loop runs zero times.
Question 3
A FOR i ← 1 TO 4
B OUTPUT "Hello"
C NEXT i
D // Hello is shown 5 times
Line A — This is an off-by-one mistake. 1 TO 4 runs 4 - 1 + 1 = 4 times. To run 5 times the line must be FOR i ← 1 TO 5.
Question 4
A FOR i ← 3 TO 1
B OUTPUT i * 2
C NEXT i
D // Output: 2 4 6
Line A — The start value 3 is bigger than the end value 1, so the loop would run zero times and print nothing. It should be FOR i ← 1 TO 3, which runs 3 times.
Question 5
A Runs ← 0
B FOR i ← 2 TO 6
C Runs ← Runs + 1
D NEXT Runs
E OUTPUT Runs
Line D — NEXT must name the loop variable used in the FOR line. It should be NEXT i. The loop runs 6 - 2 + 1 = 5 times, so Runs ends as 5.

Student with a class code? Join your class →