← IGCSE Computer Science Paper 2

A WHILE loop ended by a rogue value

Loop that repeats until the user types a rogue (sentinel) value such as -1. INPUT before the loop and again at the end of the body; the rogue value is not counted or totalled.

Understanding A WHILE loop ended by a rogue value in IGCSE Computer Science Paper 2

What it does: The INPUT at the end of the body fetches the next value, which the WHILE condition then checks. Without it the loop would never end. A rogue value is a special value that is not real data. It tells the loop to stop.

Common mistakes: The total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be Total <- 0. The first INPUT before the loop reads into the wrong variable. Mrk is not Mark, so the loop test uses a value that was never read. It should be INPUT Mark.

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 WHILE loop reads numbers until the user types -1. What is -1 called?
A counterA rogue (sentinel) value
Matches
WHILE Number <> -1 DO INPUT Number Total <- Total + Number ENDWHILE
↓
INPUT is at the start of the body, so the rogue value -1 is added to the total before the loop condition is checked. It should be read at the end of the body.
Applying
Meanings
Term
WHILE Number <> -1 DO
Meaning
Loop that repeats until the user types a rogue (sentinel) value such as -1. INPUT before the loop and again at the end of the body; the rogue value is not counted or totalled.
Practice
Total <- 0
Testing
Gaps
WHILE Number <> DO
Check Answer
Spots
1Total <- 1
2INPUT Number

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

The 'Jellyfish' badge — Flow Controller, 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 WHILE loop reads numbers until the user types -1. What is -1 called?
A. A rogue (sentinel) value ✓
B. A counter
A rogue value is a special value that is not real data. It tells the loop to stop.
2. Which layout reads input correctly for a rogue-value loop?
A. INPUT Number, then WHILE Number <> -1 DO, then INPUT Number again at the end of the body ✓
B. WHILE Number <> -1 DO, then INPUT Number only at the start of the body
The first INPUT must come before the loop so the condition has a value to test. A second INPUT at the end of the body reads the next value.
3. Why is there an INPUT before the WHILE loop starts?
A. So the loop can count how many times it runs
B. So the condition has a first value to test ✓
A WHILE loop tests its condition before running the body. Number needs a value first.
4. Which condition keeps the loop running until the user types STOP?
A. WHILE Name = "STOP" DO
B. WHILE Name <> "STOP" DO ✓
The loop repeats while the input is NOT the rogue value. With = the loop would not run at all unless the first input was STOP.
5. A user types 5, 8, 2 and then -1. The loop adds each number to Total. What is Total?
A. 15 ✓
B. 14
The rogue value -1 is not real data, so it is not added. 5 + 8 + 2 = 15.

Match the Pairs

WHILE Number <> -1 DO → Keeps looping until the user types the rogue value -1, which signals the end of the input and is not real data
INPUT Number WHILE Number <> -1 DO → The first value is read before the loop so the WHILE condition has something to test, even if it is the rogue value straight away
Read the next value at the end of the loop body so the rogue value can be checked on the next test → WHILE Number <> -1 DO Total <- Total + Number INPUT Number ENDWHILE
A text rogue value such as STOP ends the loop and is never added to the total or counted → INPUT Word WHILE Word <> "STOP" DO Count <- Count + 1 INPUT Word ENDWHILE
WHILE Number <> -1 DO INPUT Number Total <- Total + Number ENDWHILE → INPUT is at the start of the body, so the rogue value -1 is added to the total before the loop condition is checked. It should be read at the end of the body.
WHILE Number <> -1 DO Total <- Total + Number ENDWHILE → There is no INPUT before the loop or inside it, so Number never changes and the loop never ends (or Number has no value to test).

Key Term

WHILE Number <> -1 DO
Loop that repeats until the user types a rogue (sentinel) value such as -1. INPUT before the loop and again at the end of the body; the rogue value is not counted or totalled.

Fill in the Blanks

1. INPUT Number WHILE Number <> -1 DO Total <- Total + Number
Hint: Type the rogue (sentinel) value that tells the loop to stop. It is not real data.
2. INPUT Number WHILE Number <> -1 DO Total <- Total + Number
Hint: Type the keyword that starts a pre-condition loop. It checks the rogue value before each pass.
3. Total <- Total + Number INPUT Number ENDWHILE
Hint: Type the statement that reads the next value at the end of the loop body.
4. INPUT Number WHILE Number <> -1 DO Count <- Count + 1
Hint: Type the relational operator meaning 'not equal to', so the loop runs while the value is not the rogue value.
5. WHILE Number <> -1 DO Count <- Count + 1 INPUT Number
Hint: Type the variable that holds the running count of valid values. The rogue value is never counted.

Spot the Error

Question 1
A Total <- 1
B INPUT Number
C WHILE Number <> -1 DO
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Line A — The total must start at 0. Starting at 1 adds an extra 1 to the answer. It should be Total <- 0.
Question 2
A DECLARE Mark : INTEGER
B INPUT Mrk
C WHILE Mark <> -1 DO
D OUTPUT Mark
E INPUT Mark
F ENDWHILE
Line B — The first INPUT before the loop reads into the wrong variable. Mrk is not Mark, so the loop test uses a value that was never read. It should be INPUT Mark.
Question 3
A INPUT Number
B Total <- 0
C WHILE Number = -1 DO
D Total <- Total + Number
E INPUT Number
F ENDWHILE
Line C — The loop must keep going while Number is NOT the rogue value. With = it would only run when -1 is typed, so real data is skipped. It should be WHILE Number <> -1 DO.
Question 4
A Count <- 0
B INPUT Mark
C WHILE Mark <> -1
D Count <- Count + 1
E INPUT Mark
F ENDWHILE
Line C — A WHILE loop header must end with DO. It should be WHILE Mark <> -1 DO.
Question 5
A Sum <- 0
B INPUT Score
C WHILE Score <> -1 DO
D Sum <- Score
E INPUT Score
F ENDWHILE
Line D — This replaces the sum each time instead of adding to it, so only the last score is kept. It should be Sum <- Sum + Score.

Student with a class code? Join your class →