← IGCSE Computer Science Paper 2

Choosing FOR, WHILE or REPEAT

Use FOR when the number of repeats is known. Use WHILE (tests at the start) or REPEAT (tests at the end) when repeating depends on a condition.

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

What it does: WHILE checks its condition before each repeat. If the condition is false at the start, the body is skipped completely. When the repeat count is fixed and known, a count-controlled FOR loop is the best choice. We do not know how many attempts the user will need, so the repeating depends on a condition. The number of marks is known (30), so FOR is the right choice. The number of rounds is unknown and the question must be asked at least once, so REPEAT (test at end) suits it. WHILE tests first, so it can skip the body if the first input is -1. FOR needs a known count and a counter, not a sentinel value.

Common mistakes: A FOR loop ends with NEXT and the loop variable, not ENDFOR. The line should be NEXT Student. A WHILE loop (condition-controlled) must end with ENDWHILE. NEXT is only used to close a FOR loop.

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 265–274) and word search (Modules 254–274).

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 numbers 1 to 10. Which loop is the best choice?
WHILE Count > 10 DOFOR Count ← 1 TO 10
Matches
WHILE Total < 100 DO INPUT Num Total ← Total + Num NEXT
↓
A WHILE loop must end with ENDWHILE, not NEXT. NEXT belongs to a FOR loop.
Applying
Meanings
Term
FOR, WHILE, REPEAT
Meaning
Use FOR when the number of repeats is known. Use WHILE (tests at the start) or REPEAT (tests at the end) when repeating depends on a condition.
Practice
FOR Count ← 1 TO 5
Testing
Gaps
Count ← 1 TO 5
Check Answer
Spots
3 OUTPUT Mark
4ENDFOR

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

The 'Kraken' badge — Deep Sea 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 numbers 1 to 10. Which loop is the best choice?
A. FOR Count ← 1 TO 10 ✓
B. WHILE Count > 10 DO
The number of repeats is known (10), so a count-controlled FOR loop is best.
2. A program keeps asking for a password until the user types the right one. Which type of loop fits?
A. A condition-controlled loop (WHILE or REPEAT) ✓
B. A count-controlled loop (FOR)
We do not know how many attempts the user will need, so the repeating depends on a condition.
3. Which statement about REPEAT...UNTIL is true?
A. The condition is tested at the start, so the loop body may never run
B. The condition is tested at the end, so the loop body runs at least once ✓
REPEAT tests at the end. WHILE is the loop that tests at the start.
4. Which statement about WHILE...DO...ENDWHILE is true?
A. The condition is tested at the end, so the body always runs once
B. The condition is tested at the start, so the body might never run ✓
WHILE checks the condition before each repeat. If it is false straight away, the body is skipped.
5. A program must process exactly 30 test marks stored in an array. Which loop should you choose?
A. FOR Index ← 1 TO 30 ✓
B. REPEAT ... UNTIL the user stops typing
The number of marks is known (30), so FOR is the right choice.

Match the Pairs

FOR Count ← 1 TO 10 → A count-controlled loop: the number of repeats is known (10) before it starts
REPEAT ... UNTIL Guess = Secret → A condition-controlled loop that tests at the end, so the body always runs at least once
The condition is tested at the start, so the loop body may never run → WHILE Password <> "open" DO
The number of repeats is known: read exactly 5 marks → FOR Index ← 1 TO 5
WHILE Total < 100 DO INPUT Num Total ← Total + Num NEXT → A WHILE loop must end with ENDWHILE, not NEXT. NEXT belongs to a FOR loop.
FOR Count ← 1 TO Answer <> 5 → FOR needs a known number as its end value, not a condition. A condition-based repeat needs WHILE or REPEAT.

Key Term

FOR, WHILE, REPEAT
Use FOR when the number of repeats is known. Use WHILE (tests at the start) or REPEAT (tests at the end) when repeating depends on a condition.

Fill in the Blanks

1. FOR Count ← 1 TO 5 OUTPUT Count NEXT Count
Hint: Type the loop keyword used when the number of repeats is known in advance.
2. WHILE Number < 0 DO INPUT Number ENDWHILE
Hint: Type the keyword for a condition-controlled loop that tests its condition at the start.
3. REPEAT INPUT Guess UNTIL Guess = 7
Hint: Type the keyword that ends a loop which tests its condition at the end.
4. WHILE Lives > 0 DO Lives ← Lives - 1 ENDWHILE
Hint: Type the keyword that closes a loop which tests its condition before each repeat.
5. FOR Day ← 1 TO 7 OUTPUT Day NEXT Day
Hint: Type the keyword that closes a count-controlled loop.

Spot the Error

Question 1
A FOR Student ← 1 TO 30
B INPUT Mark
C OUTPUT Mark
D ENDFOR
Line D — A FOR loop ends with NEXT and the loop variable, not ENDFOR. The line should be NEXT Student.
Question 2
A INPUT Password
B WHILE Password <> "open" DO
C INPUT Password
D NEXT Password
Line D — A WHILE loop (condition-controlled) must end with ENDWHILE. NEXT is only used to close a FOR loop.
Question 3
A Guess ← 0
B WHILE
C INPUT Guess
D UNTIL Guess = 7
Line B — The loop ends with UNTIL, so it must start with REPEAT, not WHILE. The line should be REPEAT.
Question 4
A Total ← 0
B INPUT Mark
C WHILE Mark = -1 DO
D Total ← Total + Mark
E INPUT Mark
F ENDWHILE
Line C — WHILE repeats while its condition is true. With = -1 the loop would not run for normal marks. It should be WHILE Mark <> -1 DO.
Question 5
A Total ← 0
B FOR Num = 1 TO 10
C Total ← Total + Num
D NEXT Num
Line B — A FOR loop sets its start value with the assignment arrow ←, not =. It should be FOR Num ← 1 TO 10.

Student with a class code? Join your class →