← IGCSE Computer Science Paper 2

Choosing count-controlled, pre-condition or post-condition loops

Choose FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT...UNTIL when the loop must run at least once.

Understanding Choosing count-controlled, pre-condition or post-condition loops in IGCSE Computer Science Paper 2

What it does: WHILE tests the condition before the body, so if the first input is the rogue value the loop never runs. Validation needs the input to be taken once before it can be checked, so REPEAT...UNTIL is the best choice. The number of repeats (12) is known in advance, so a count-controlled FOR loop is the best choice.

Common mistakes: The number of repeats is known (10), so FOR is the right loop. A FOR loop uses TO to give the end value, not UNTIL. It should be FOR Count <- 1 TO 10. A WHILE loop that reads until a rogue value must keep going while the value is NOT the rogue value. It should be WHILE Score <> -1 DO. As written, the loop never runs for normal scores.

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 379–388) and word search (Modules 379–398).

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 the 12 times table (12 lines of output). Which loop fits best?
WHILE Count <> 12 with no counter setFOR Count <- 1 TO 12
Matches
WHILE Count <= 10 DO OUTPUT Count ENDWHILE
↓
Count is never given a start value and never increased, so the loop never ends (or never starts). The number of repeats is known, so a FOR loop is the better choice: FOR Count <- 1 TO 10.
Applying
Meanings
Term
FOR, WHILE or REPEAT
Meaning
Choose FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT...UNTIL when the loop must run at least once.
Practice
// Ask at least once, so use REPEAT
Testing
Gaps
Count ← 1 TO 10
Check Answer
Spots
1FOR Count <- 1 UNTIL 10
2 INPUT Mark

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

The 'Peregrine Falcon' badge — Operator Ace, 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 the 12 times table (12 lines of output). Which loop fits best?
A. FOR Count <- 1 TO 12 ✓
B. WHILE Count <> 12 with no counter set
The number of repeats (12) is known in advance, so a count-controlled FOR loop is the best choice.
2. A program reads marks until the user types -1 (a rogue value). Which loop is best?
A. WHILE Mark <> -1 DO ✓
B. FOR Mark <- 1 TO -1
We do not know how many marks will be entered, so we use a WHILE loop that stops at the rogue value.
3. A password must be asked for at least once, and again if it is wrong. Which loop is best?
A. FOR Attempt <- 1 TO 0
B. REPEAT ... UNTIL Password = Correct ✓
The input must happen at least once, so a post-condition REPEAT...UNTIL loop is ideal. It checks the condition after the body has run.
4. Which statement about a WHILE loop is true?
A. It always runs at least once
B. It may run zero times if the condition is false at the start ✓
WHILE tests the condition before the body (pre-condition), so the body can be skipped completely. REPEAT...UNTIL always runs at least once.
5. Which statement about REPEAT...UNTIL is true?
A. The condition is checked after the loop body has run ✓
B. The condition is checked before the loop body runs
REPEAT...UNTIL is a post-condition loop, so the body always runs at least once before the test.

Match the Pairs

FOR Count <- 1 TO 30 INPUT Mark NEXT Count → A count-controlled loop: use it when the number of repeats is known in advance, such as 30 students.
REPEAT INPUT Password UNTIL Password = "open" → A post-condition loop: use it when the body must run at least once, such as asking for input before checking it.
Read numbers until the rogue value -1 is entered; the loop may run zero times if -1 is typed first, so use a pre-condition loop. → INPUT Num WHILE Num <> -1 DO Total <- Total + Num INPUT Num ENDWHILE
Validate an input so that the user is always asked at least once and asked again until the value is acceptable. → REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120
WHILE Count <= 10 DO OUTPUT Count ENDWHILE → Count is never given a start value and never increased, so the loop never ends (or never starts). The number of repeats is known, so a FOR loop is the better choice: FOR Count <- 1 TO 10.
FOR Attempt <- 1 TO 100 INPUT Password NEXT Attempt → A FOR loop is the wrong choice because the number of attempts is not known in advance. The user should be asked until the password is correct, so use REPEAT...UNTIL Password = "open".

Key Term

FOR, WHILE or REPEAT
Choose FOR when the number of repeats is known, WHILE when the loop may run zero times, and REPEAT...UNTIL when the loop must run at least once.

Fill in the Blanks

1. FOR Count ← 1 TO 10 OUTPUT Count NEXT Count
Hint: Type the loop keyword used when you know in advance exactly how many times to repeat.
2. WHILE Num <> -1 DO Total ← Total + Num ENDWHILE
Hint: Type the loop keyword that checks the condition first, so the loop may run zero times until a rogue value.
3. REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 120
Hint: Type the keyword that ends a post-condition validation loop and holds the condition.
4. REPEAT INPUT Password UNTIL Password = "open"
Hint: Type the keyword that starts the loop which must always run at least once before checking.
5. FOR Student ← 1 TO 30 INPUT Mark NEXT Student
Hint: Type the keyword that ends a count-controlled loop for a known number of students.

Spot the Error

Question 1
A FOR Count <- 1 UNTIL 10
B INPUT Mark
C Total <- Total + Mark
D NEXT Count
Line A — The number of repeats is known (10), so FOR is the right loop. A FOR loop uses TO to give the end value, not UNTIL. It should be FOR Count <- 1 TO 10.
Question 2
A INPUT Score
B WHILE Score = -1 DO
C Total <- Total + Score
D INPUT Score
E ENDWHILE
Line B — A WHILE loop that reads until a rogue value must keep going while the value is NOT the rogue value. It should be WHILE Score <> -1 DO. As written, the loop never runs for normal scores.
Question 3
A OUTPUT "Enter age 0-120"
B WHILE
C INPUT Age
D UNTIL Age >= 0 AND Age <= 120
Line B — Input validation must ask at least once, so a REPEAT...UNTIL loop is used. The loop must start with REPEAT to match the UNTIL at the end. WHILE needs DO and ENDWHILE.
Question 4
A Count <- 0
B WHILE Count < 5 DO
C Count <- Count + 1
D NEXT Count
Line D — NEXT closes a FOR loop. A WHILE loop must be closed with ENDWHILE.
Question 5
A REPEAT
B INPUT Password
C Tries <- Tries + 1
D ENDWHILE
Line D — A post-condition loop ends with UNTIL and its condition, not ENDWHILE. The correct line is UNTIL Password = Secret. It repeats until the password matches.

Student with a class code? Join your class →