← IGCSE Computer Science Paper 2

Error: wrong loop bounds

A FOR loop with the wrong start or end value runs one time too many or too few, e.g. TO 9 instead of TO 10, or starting at 0 for a 1-based array. Check the bounds with a trace table.

Understanding Error: wrong loop bounds in IGCSE Computer Science Paper 2

What it does: The bounds 1 and 10 match the first and last indexes of the array, so the loop runs exactly 10 times. Count the start value too: 0, 1, 2, 3, 4 is 5 values. Number of runs = end - start + 1. 1 TO 8 gives 8 passes. 1 TO 7 gives only 7, which is an off-by-one error. An off-by-one error is a wrong bound that changes the number of repeats by exactly one. A loop that never stops is a different error. IGCSE arrays usually start at index 1, so starting the loop at 0 is an off-by-one error with the start value.

Common mistakes: This is an off-by-one error. The comment says to add 1 to 10, but TO 9 stops one time too soon, so 10 is never added. It should be FOR Count ← 1 TO 10. The array is 1-based, so index 0 does not exist. Starting at 0 also makes the loop run 6 times instead of 5. It should be FOR Index ← 1 TO 5.

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 232–241) and word search (Modules 232–253).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A program should output the numbers 1 to 10. Which FOR line is correct?
FOR Count ← 1 TO 10FOR Count ← 1 TO 9
Matches
FOR Num ← 1 TO 9 Total ← Total + Num NEXT Num // should add the numbers 1 to 10
↓
The end value is 9, so the loop stops one time too early and 10 is never added. It should be TO 10.
Applying
Meanings
Term
off-by-one error
Meaning
A FOR loop with the wrong start or end value runs one time too many or too few, e.g. TO 9 instead of TO 10, or starting at 0 for a 1-based array. Check the bounds with a trace table.
Practice
Total ← 0
Testing
Gaps
FOR Count ← 1 TO
Check Answer
Spots
3// add the numbers 1 to 10
4FOR Count ← 1 TO 9

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

The 'Woolly Mammoth' badge — Tundra Master, 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. A program should output the numbers 1 to 10. Which FOR line is correct?
A. FOR Count ← 1 TO 10 ✓
B. FOR Count ← 1 TO 9
TO 9 stops one time too early, so the loop runs 9 times instead of 10. This is an off-by-one error.
2. What happens when this loop runs? FOR Num ← 1 TO 9 (the task is to process 10 numbers)
A. It runs 9 times, one too few ✓
B. It runs 10 times, as needed
A FOR loop from 1 to 9 runs 9 times (9 - 1 + 1). The last number is never processed.
3. An array Scores[1:5] is 1-based. Which loop visits every element without going out of range?
A. FOR Index ← 1 TO 5 ✓
B. FOR Index ← 0 TO 5
Starting at 0 means Scores[0] is used, but that element does not exist in a 1-based array. The loop also runs 6 times, not 5.
4. Which statement about FOR Index ← 0 TO 4 is true?
A. It runs 4 times, because the end value is 4
B. It runs 5 times, so it is an off-by-one error if the task needs 4 items ✓
Count the start value too: 0, 1, 2, 3, 4 is 5 values. Number of runs = end - start + 1.
5. Which tool is best for checking whether a loop's start and end values are correct?
A. Adding more comments to the loop
B. A trace table showing the loop variable on each pass ✓
A trace table shows the first and last values of the loop variable, so you can spot a loop that runs one time too many or too few.

Match the Pairs

FOR Count ← 1 TO 9 OUTPUT Count NEXT Count → Runs 9 times, not 10, so it misses the last value. This is an off-by-one error if 10 repeats were needed.
FOR Index ← 0 TO 4 OUTPUT Scores[Index] NEXT Index → Starts at 0 on a 1-based array, so Scores[0] does not exist and the loop makes an extra pass.
To output all 10 values in a 1-based array of 10 items, the loop must start at 1 and end at 10. → FOR Index ← 1 TO 10 OUTPUT Marks[Index] NEXT Index
A trace table of a loop shows the value of the counter on each pass and reveals whether the last value is missed. → FOR Count ← 1 TO 5 Total ← Total + Count NEXT Count
FOR Num ← 1 TO 9 Total ← Total + Num NEXT Num // should add the numbers 1 to 10 → The end value is 9, so the loop stops one time too early and 10 is never added. It should be TO 10.
DECLARE Names : ARRAY[1:6] OF STRING FOR Index ← 0 TO 6 OUTPUT Names[Index] NEXT Index → The loop starts at 0 but the array starts at 1, so it makes one extra pass. It should be FOR Index ← 1 TO 6.

Key Term

off-by-one error
A FOR loop with the wrong start or end value runs one time too many or too few, e.g. TO 9 instead of TO 10, or starting at 0 for a 1-based array. Check the bounds with a trace table.

Fill in the Blanks

1. FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Hint: Type the end value that makes this loop run exactly ten times. Too small an end value is an off-by-one error.
2. FOR Index ← 1 TO 5 OUTPUT Scores[Index] NEXT Index
Hint: Scores is a 1-based array with five elements. Type the start value that matches its first element.
3. FOR Index ← 0 TO 4 OUTPUT Names[Index] NEXT Index
Hint: Names is a 0-based array holding five names. Type the last valid index so the loop does not run one time too many.
4. FOR Count ← 1 TO 3 OUTPUT Count * 2 NEXT Count
Hint: A trace table for this loop must show three rows, one for each pass. Type the end value that gives three passes.
5. FOR Student ← 1 TO 20 INPUT Marks[Student] NEXT Student
Hint: There are twenty students stored in a 1-based array. Type the end value that reads in every mark and no more.

Spot the Error

Question 1
A DECLARE Total : INTEGER
B Total ← 0
C // add the numbers 1 to 10
D FOR Count ← 1 TO 9
E Total ← Total + Count
F NEXT Count
Line D — This is an off-by-one error. The comment says to add 1 to 10, but TO 9 stops one time too soon, so 10 is never added. It should be FOR Count ← 1 TO 10.
Question 2
A // Mark holds indexes 1 to 5
B Sum ← 0
C FOR Index ← 0 TO 5
D Sum ← Sum + Mark[Index]
E NEXT Index
Line C — The array is 1-based, so index 0 does not exist. Starting at 0 also makes the loop run 6 times instead of 5. It should be FOR Index ← 1 TO 5.
Question 3
A // Score has indexes 1 to 10
B Sum ← 0
C // add every score
D FOR Index ← 1 TO 11
E Sum ← Sum + Score[Index]
F NEXT Index
Line D — Score only has indexes 1 to 10. TO 11 makes the loop run one time too many and reads Score[11], which is out of range. It should be FOR Index ← 1 TO 10.
Question 4
A // show the 7 times table
B // from 7 * 1 up to 7 * 12
C FOR Number ← 2 TO 12
D OUTPUT 7 * Number
Line C — The table should start at 7 * 1, but starting at 2 skips the first line and the loop runs 11 times instead of 12. It should be FOR Number ← 1 TO 12.
Question 5
A // Rain has indexes 1 to 7
B FOR Day ← 1 TO 6
C OUTPUT Rain[Day]
D NEXT Day
Line B — There are 7 days of rain data, but TO 6 makes the loop run one time too few, so Rain[7] is never output. It should be FOR Day ← 1 TO 7.

Student with a class code? Join your class →