← IGCSE Computer Science Paper 2

Tracing a validation algorithm

Step through a validation loop with valid and invalid inputs, recording each rejected value, the error message output and the value finally accepted in a trace table.

Understanding Tracing a validation algorithm in IGCSE Computer Science Paper 2

What it does: 150 and -4 are abnormal data, so the loop body runs twice and outputs the error message twice. 35 is normal data, so the loop ends and 35 is accepted. Each INPUT inside the loop gives a new value for Age, so a new row or entry is recorded. The last value in the column is the accepted one. Messages displayed by the program are OUTPUT. INPUT is only for values the user types. The loop repeats until a valid value is entered, however many attempts that takes. The UNTIL condition is the range check. The loop only stops when it is true, so every invalid input makes the loop repeat.

Common mistakes: Line 2 must read the first attempt with INPUT Age. OUTPUT only displays a value, so the loop would test an Age that was never entered. The trace table's input column would stay empty. The variable must be Password, the same name used in the length check and the UNTIL condition. Pass is a different variable, so the tested value never matches the value entered. It should be INPUT Password.

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 83–92) and word search (Modules 71–92).

Take this worksheet further — free with Cloodit

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

Learning
Duo
A validation loop asks for a mark from 0 to 100. A student enters 150, then 85. What does the trace table show for the INPUT column?
150 on one row, then 85 on the next rowOnly 85, because 150 was rejected so it is not recorded
Matches
Trace of inputs 150, -4, 35 (range 1 to 100): the Age column shows 150, 35 and the output column shows one error message.
↓
The rejected value -4 was left out of the trace. Every attempt must be recorded, so there should be two error messages, one for 150 and one for -4, before 35 is accepted.
Applying
Meanings
Term
Tracing a validation loop
Meaning
Step through a validation loop with valid and invalid inputs, recording each rejected value, the error message output and the value finally accepted in a trace table.
Practice
// Trace with inputs 0, 25, 12
Testing
Gaps
Age >= 1 AND Age <= 100
Check Answer
Spots
1OUTPUT "Enter age 1-100"
2OUTPUT Age

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 validation loop asks for a mark from 0 to 100. A student enters 150, then 85. What does the trace table show for the INPUT column?
A. 150 on one row, then 85 on the next row ✓
B. Only 85, because 150 was rejected so it is not recorded
Every value entered is recorded in the INPUT column, including rejected ones. The trace table shows each attempt in order.
2. In a trace of a validation loop, the user enters -5 and it fails the range check. Where does the error message go?
A. In the OUTPUT column on the same row as the -5 attempt ✓
B. In the INPUT column next to the -5
Messages displayed by the program are OUTPUT. INPUT is only for values the user types.
3. The loop is REPEAT ... UNTIL Mark >= 0 AND Mark <= 100. The inputs are 120, -3, 50. How many times does the loop body run?
A. 2 times: only the rejected values make the loop run
B. 3 times: two rejected values and one accepted value ✓
The body runs once for each input. The loop ends after the third pass, when 50 is accepted.
4. Which set of test inputs for a trace of a range check 1 to 10 uses both normal and abnormal data?
A. 3, 7, 10
B. 15, 0, 7 ✓
15 and 0 are abnormal because they are outside the range and are rejected. 7 is normal and is accepted. The second set has only valid values, so the error message is never traced.
5. When does a validation loop stop repeating?
A. When the input value passes the check ✓
B. After the error message has been output once
The loop repeats until a valid value is entered, however many attempts that takes.

Match the Pairs

REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100 → A validation loop: it keeps asking for Age until the value is inside the range 1 to 100, so each rejected attempt gets its own row in the trace.
OUTPUT "Invalid age" → The error message. In the trace table it is written in the OUTPUT column on the row of each rejected value, and not on the row of the accepted value.
Tracing a validation loop with the inputs 150, -4, 35 for a range check 1 to 100 gives two rejected values and 35 as the value finally accepted. → WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age" INPUT Age ENDWHILE
In a trace table the Age column lists every attempt in order, so a row is added for each INPUT the loop asks for until a valid value is entered. → INPUT Age WHILE Age < 1 OR Age > 100 DO INPUT Age ENDWHILE
Trace of inputs 150, -4, 35 (range 1 to 100): the Age column shows 150, 35 and the output column shows one error message. → The rejected value -4 was left out of the trace. Every attempt must be recorded, so there should be two error messages, one for 150 and one for -4, before 35 is accepted.
Trace of inputs 150, -4, 35 (range 1 to 100): the output column shows an error message on the row where Age is 35. → 35 is valid normal data, so no error message is output for it. The messages belong on the rows for 150 and -4 only, and 35 is the accepted value that ends the loop.

Key Term

Tracing a validation loop
Step through a validation loop with valid and invalid inputs, recording each rejected value, the error message output and the value finally accepted in a trace table.

Fill in the Blanks

1. REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100
Hint: Type the keyword that ends the loop and holds the condition for a valid value.
2. WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age" INPUT Age
Hint: Type the logical operator that rejects a value if either limit is broken.
3. INPUT Age WHILE Age < 1 OR Age > 100 DO OUTPUT "Invalid age"
Hint: Type the command that shows the error message, which is recorded in the OUTPUT column of the trace table.
4. REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 50
Hint: Type the variable name that is tested in the condition and gets a new value each time round the loop.
5. REPEAT INPUT Score UNTIL Score >= 0 AND Score <= 100
Hint: Type the relational operator that accepts the lower limit as a valid value.

Spot the Error

Question 1
A OUTPUT "Enter age 1-100"
B OUTPUT Age
C WHILE Age < 1 OR Age > 100 DO
D OUTPUT "Invalid"
E INPUT Age
F ENDWHILE
Line B — Line 2 must read the first attempt with INPUT Age. OUTPUT only displays a value, so the loop would test an Age that was never entered. The trace table's input column would stay empty.
Question 2
A REPEAT
B INPUT Pass
C IF LENGTH(Password) < 8 THEN
D OUTPUT "Too short"
E ENDIF
F UNTIL LENGTH(Password) >= 8
Line B — The variable must be Password, the same name used in the length check and the UNTIL condition. Pass is a different variable, so the tested value never matches the value entered. It should be INPUT Password.
Question 3
A Attempts = 0
B INPUT Pin
C WHILE Pin <> 1234 DO
D Attempts ← Attempts + 1
E INPUT Pin
F ENDWHILE
Line A — In Cambridge pseudocode = is a comparison, not assignment. The counter must be set with the arrow, so line 1 should be Attempts ← 0. Without it the trace table has no starting value to count from.
Question 4
A INPUT Num
B WHILE Num < 0 AND Num > 100 DO
C OUTPUT "Invalid number"
D INPUT Num
E ENDWHILE
Line B — No number can be below 0 and above 100 at the same time, so the condition is never true. The loop would never run and invalid values would be accepted. It should use OR.
Question 5
A DECLARE Name : INTEGER
B INPUT Name
C WHILE LENGTH(Name) = 0 DO
D OUTPUT "Name required"
E INPUT Name
F ENDWHILE
Line A — A name is text, so it must be declared as STRING. INTEGER holds whole numbers only and cannot be used with LENGTH. Line 1 should be DECLARE Name : STRING.

Student with a class code? Join your class →