← IGCSE Computer Science Paper 2

Choosing the right loop

Use FOR when the number of repeats is known, WHILE when the body might run zero times (such as reading until a sentinel), and REPEAT when the body must run at least once (such as validation).

Understanding Choosing the right loop in IGCSE Computer Science Paper 2

What it does: WHILE tests the condition before the body, so if the first value is the sentinel the body runs zero times. REPEAT ... UNTIL checks at the end, so the prompt always appears at least once, which is what validation needs. The number of repeats is known (12), so a count-controlled FOR loop is the best choice.

Common mistakes: The count is known to be 5, so FOR is the right loop. But TO 4 makes it run only 4 times. It should be FOR Count ← 1 TO 5. A WHILE loop tests its condition before the first pass, so the first value must be read with INPUT Num before the loop. OUTPUT does not read a value.

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
A program must output the 12 times table, one line for each number from 1 to 12. Which loop is the best choice?
REPEAT ... UNTILFOR Count ← 1 TO 12
Matches
FOR Count ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Count // used when the number of inputs is unknown
↓
A FOR loop is used when the number of repeats is not known. A WHILE loop with a sentinel value should be used instead.
Applying
Meanings
Term
FOR / WHILE / REPEAT
Meaning
Use FOR when the number of repeats is known, WHILE when the body might run zero times (such as reading until a sentinel), and REPEAT when the body must run at least once (such as validation).
Practice
FOR Count ← 1 TO 5
Testing
Gaps
Count ← 1 TO 5
Check Answer
Spots
2OUTPUT "Enter 5 marks"
3FOR Count ← 1 TO 4

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

The 'Silverback Gorilla' badge — Savannah 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 must output the 12 times table, one line for each number from 1 to 12. Which loop is the best choice?
A. FOR Count ← 1 TO 12 ✓
B. REPEAT ... UNTIL
The number of repeats is known (12), so a count-controlled FOR loop is the best choice.
2. Which statement is correct?
A. Use FOR when the number of repeats is known in advance ✓
B. Use FOR when the user decides when to stop by typing -1
FOR is count-controlled. If the stop depends on a sentinel value typed in, use a condition-controlled loop such as WHILE.
3. A program reads marks until the user types -1 (the sentinel). The first value typed might already be -1. Which loop is best?
A. WHILE Mark <> -1 DO ✓
B. REPEAT ... UNTIL Mark = -1
WHILE tests the condition before the body, so it can run zero times. That suits a sentinel that may be entered straight away.
4. A program asks for a password and must ask at least once, repeating if it is wrong. Which loop is best?
A. WHILE Password <> Correct DO (with nothing asked first)
B. REPEAT ... UNTIL Password = Correct ✓
A validation prompt must run at least once. REPEAT tests its condition at the end, so the body always runs once.
5. Which loop can have a body that runs zero times?
A. REPEAT
B. WHILE ✓
WHILE checks its condition first, so the body may never run. REPEAT checks at the end, so the body always runs at least once.

Match the Pairs

FOR Count ← 1 TO 10 OUTPUT Count NEXT Count → A FOR loop: the number of repeats is known in advance (exactly 10 times).
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 → A REPEAT loop: the body must run at least once, so it suits a validation prompt.
Read values until a sentinel of -1 is entered, where the loop might not run at all if -1 is entered first → INPUT Num WHILE Num <> -1 DO Total ← Total + Num INPUT Num ENDWHILE
Ask for a password again and again until the entry is accepted, always asking at least once → REPEAT INPUT Password UNTIL Password = "Secret"
FOR Count ← 1 TO 5 INPUT Num Total ← Total + Num NEXT Count // used when the number of inputs is unknown → A FOR loop is used when the number of repeats is not known. A WHILE loop with a sentinel value should be used instead.
WHILE Age < 0 OR Age > 120 DO INPUT Age ENDWHILE // validation, Age not yet set → WHILE tests before the body, so the prompt may never run because Age has no value yet. REPEAT ... UNTIL should be used so the input always happens at least once.

Key Term

FOR / WHILE / REPEAT
Use FOR when the number of repeats is known, WHILE when the body might run zero times (such as reading until a sentinel), and REPEAT when the body must run at least once (such as validation).

Fill in the Blanks

1. FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Hint: Type the loop keyword used when you already know exactly how many times to repeat.
2. INPUT Mark WHILE Mark <> -1 DO INPUT Mark
Hint: Type the loop keyword that checks the condition first, so the body might run zero times when reading until a sentinel.
3. REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120
Hint: Type the keyword that ends a loop whose body runs at least once and whose condition is checked after the body.
4. FOR Day ← 1 TO 7 INPUT Temp NEXT Day
Hint: Type the keyword that closes a fixed-count loop and moves on to the next value of the counter.
5. REPEAT INPUT Pin UNTIL Pin = 1234
Hint: Type the relational operator that checks whether the entered value is equal to the correct one.

Spot the Error

Question 1
A Total ← 0
B OUTPUT "Enter 5 marks"
C FOR Count ← 1 TO 4
D INPUT Mark
E Total ← Total + Mark
F NEXT Count
Line C — The count is known to be 5, so FOR is the right loop. But TO 4 makes it run only 4 times. It should be FOR Count ← 1 TO 5.
Question 2
A OUTPUT Num
B WHILE Num <> -1 DO
C Total ← Total + Num
D INPUT Num
E ENDWHILE
Line A — A WHILE loop tests its condition before the first pass, so the first value must be read with INPUT Num before the loop. OUTPUT does not read a value.
Question 3
A REPEAT
B OUTPUT "Enter age 1-100"
C INPUT Age
D UNTIL Age >= 1 OR Age <= 100
Line D — The validation loop must repeat until the age is inside the range, so both conditions must be true. OR is true for almost any number, so it should be AND.
Question 4
A Count ← 0
B INPUT Name
C WHILE Name = "STOP" DO
D Count ← Count + 1
E INPUT Name
F ENDWHILE
Line C — The loop must keep running until the sentinel "STOP" is entered. With = the body is skipped for any other name, so it should be <>.
Question 5
A OUTPUT "Enter 1 to 10"
B WHILE
C INPUT Num
D UNTIL Num >= 1 AND Num <= 10
Line B — Validation must ask at least once, so a REPEAT loop is needed. REPEAT is closed by UNTIL, so WHILE does not match it and should be REPEAT.

Student with a class code? Join your class →