← IGCSE Computer Science Paper 2

Choosing between FOR, WHILE and REPEAT

Use FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT when it must run at least once. WHILE tests first; REPEAT tests last.

Understanding Choosing between FOR, WHILE and REPEAT in IGCSE Computer Science Paper 2

What it does: REPEAT ... UNTIL is a post-condition loop. The body runs once before the condition is tested, so it always runs at least once. The count of 12 repeats is known before the loop starts, so a count-controlled FOR loop is the best choice. FOR repeats a set number of times. WHILE is condition-controlled because it repeats until its condition changes.

Common mistakes: A FOR loop is used when the number of repeats is known, and its range is written with TO, not UNTIL. It should be FOR Count ← 1 TO 5. REPEAT...UNTIL tests at the end and stops when the condition is TRUE. The loop should stop when the PIN is correct, so it should be UNTIL Pin = 1234.

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 47–57) and word search (Modules 47–70).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A program must display a message exactly 10 times. Which loop is the best choice?
REPEAT ... UNTILFOR ... TO ... NEXT
Matches
FOR Count ← 1 TO TotalPressed INPUT Key NEXT Count (used to keep asking until the user types STOP)
↓
A FOR loop is the wrong choice because the number of repeats is not known. A condition-controlled loop such as REPEAT ... UNTIL or WHILE is needed.
Applying
Meanings
Term
FOR, WHILE or REPEAT
Meaning
Use FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT when it must run at least once. WHILE tests first; REPEAT tests last.
Practice
// REPEAT runs at least once
Testing
Gaps
Count ← 1 TO 10
Check Answer
Spots
1Total ← 0
2FOR Count ← 1 UNTIL 5

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

The 'Leopard' badge — Savannah Perfectionist, 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 display a message exactly 10 times. Which loop is the best choice?
A. REPEAT ... UNTIL
B. FOR ... TO ... NEXT ✓
The number of repeats is known (10), so it is count-controlled. FOR is the best fit.
2. A loop might need to run zero times, for example if the user has no items to enter. Which loop is the best choice?
A. WHILE ... DO ... ENDWHILE ✓
B. REPEAT ... UNTIL
WHILE tests its condition before the first pass, so the body can be skipped completely. REPEAT always runs at least once.
3. A password must be asked for at least once, then asked again until it is correct. Which loop is the best choice?
A. REPEAT ... UNTIL ✓
B. FOR ... TO ... NEXT
The user must be asked at least once, so a post-condition loop (REPEAT) is best. The number of attempts is not known, so FOR does not fit.
4. Which statement is true?
A. REPEAT tests its condition first; WHILE tests its condition last.
B. WHILE tests its condition first; REPEAT tests its condition last. ✓
WHILE is a pre-condition loop. REPEAT is a post-condition loop, so its body always runs at least once.
5. Which loop is count-controlled?
A. FOR Count ← 1 TO 20 ✓
B. WHILE Total < 100 DO
FOR repeats a set number of times. WHILE is condition-controlled because it repeats until its condition changes.

Match the Pairs

FOR Count ← 1 TO 10 → A count-controlled loop: use it when the number of repeats is known before the loop starts
WHILE Password <> "open" DO → A pre-condition loop: the test happens first, so the body may run zero times
A loop must run at least once, such as asking for a password before it can be checked, with the test at the end → REPEAT INPUT Password UNTIL Password = "open"
A scenario where the number of repeats is known: output the 12 times table → FOR Number ← 1 TO 12 OUTPUT Number * 7 NEXT Number
FOR Count ← 1 TO TotalPressed INPUT Key NEXT Count (used to keep asking until the user types STOP) → A FOR loop is the wrong choice because the number of repeats is not known. A condition-controlled loop such as REPEAT ... UNTIL or WHILE is needed.
REPEAT OUTPUT Item Count ← Count + 1 UNTIL Count > Total (Total could be 0 and nothing should print) → REPEAT always runs at least once, so it would output an item even when Total is 0. A WHILE loop should be used because it can run zero times.

Key Term

FOR, WHILE or REPEAT
Use FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT when it must run at least once. WHILE tests first; REPEAT tests last.

Fill in the Blanks

1. FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Hint: Type the keyword for a count-controlled loop used when the number of repeats is known in advance.
2. WHILE Stock > 0 DO Stock ← Stock - 1 ENDWHILE
Hint: Type the keyword for the loop that tests its condition first, so it may run zero times.
3. REPEAT INPUT Guess UNTIL Guess = 7
Hint: Type the keyword that starts the loop that must always run at least once.
4. REPEAT INPUT Guess UNTIL Guess = Secret
Hint: Type the keyword that ends a loop by testing the condition last (a post-condition).
5. FOR Day ← 1 TO 7 OUTPUT Day NEXT Day
Hint: Type the keyword that closes the count-controlled loop and moves on to the next value of the counter.

Spot the Error

Question 1
A Total ← 0
B FOR Count ← 1 UNTIL 5
C INPUT Mark
D Total ← Total + Mark
E NEXT Count
Line B — A FOR loop is used when the number of repeats is known, and its range is written with TO, not UNTIL. It should be FOR Count ← 1 TO 5.
Question 2
A REPEAT
B INPUT Pin
C UNTIL Pin <> 1234
Line C — REPEAT...UNTIL tests at the end and stops when the condition is TRUE. The loop should stop when the PIN is correct, so it should be UNTIL Pin = 1234.
Question 3
A INPUT Mark
B WHILE Mark >= 0 THEN
C Total ← Total + Mark
D INPUT Mark
E ENDWHILE
Line B — A WHILE loop header ends with DO, not THEN (THEN belongs to IF). It should be WHILE Mark >= 0 DO.
Question 4
A FOR Day ← 1 TO 7
B INPUT Steps
C Total ← Total + Steps
D ENDFOR
Line D — A FOR loop is closed with NEXT followed by the loop variable, not ENDFOR. It should be NEXT Day.
Question 5
A WHILE Choice <> 0 DO
B OUTPUT "1 Play 0 Quit"
C INPUT Choice
D UNTIL Choice = 0
Line A — The menu must be shown at least once, so REPEAT is the right loop. WHILE tests first, before Choice has any value, and would not match the UNTIL at the end. Line 1 should be REPEAT.

Student with a class code? Join your class →