Skip to content
IGCSE·Tuition
Computer Science · Lesson

Describe input validation without language-specific shortcuts

You know what a range check is, but writing it as a loop in an exam answer is a different skill.

On this page
  1. How do I turn a rule into a loop?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where this leads next

Input validation is a loop that keeps asking until the value meets a rule. The rule might be a range, a length or a presence check, and the loop condition describes the invalid case.

This lesson belongs to pseudocode for the applicable syllabus. It builds on tracing assignment and comparison, and connects to the wider ideas in validation, verification and testing.

How do I turn a rule into a loop?

State the rule in words, then write the invalid condition, because the loop repeats while the data is wrong.

CheckRuleInvalid condition
RangeAge from 12 to 18Age < 12 OR Age > 18
LengthCode has exactly 6 charactersLENGTH(Code) <> 6
PresenceName is not emptyName = ""

The pattern is: read once, then repeat while invalid, asking again inside the loop.

INPUT Age
WHILE Age < 12 OR Age > 18 DO
   OUTPUT "Enter an age from 12 to 18"
   INPUT Age
ENDWHILE

Worked example

Trace the loop above when the user types 25, then 9, then 14.

StepAgeCondition (Age < 12 OR Age > 18)Action
first INPUT2525 > 18, so TRUEprompt, INPUT again
second INPUT99 < 12, so TRUEprompt, INPUT again
third INPUT14FALSE, FALSE, so FALSEloop ends

The prompt is output twice and Age finishes as 14. Check: 14 is not below 12 and not above 18, so it is allowed.

Some questions also accept the check in words: “Repeat the input while the age is less than 12 or more than 18.” Name the rule precisely and avoid pointing to a built-in method.

The mistake to watch for

A common slip is using AND where OR is needed.

Mistaken condition: WHILE Age < 12 AND Age > 18 DO

No number is both below 12 and above 18, so the condition is always FALSE. The loop never runs, and 25 is accepted.

The correction is OR for the invalid case. A second slip is using the valid condition in a WHILE: WHILE Age >= 12 AND Age <= 18 repeats the prompt for good values and stops for bad ones.

If you prefer to write the valid rule, use REPEAT ... UNTIL Age >= 12 AND Age <= 18.

Check yourself

1. Write the invalid condition for a mark that must be from 0 to 100.

Show answer

Mark < 0 OR Mark > 100

2. A code must have exactly 5 characters. The user types “AB12”. Does the loop repeat?

Show answer

LENGTH(“AB12”) is 4, and 4 <> 5 is TRUE. Yes, the loop repeats and asks again.

3. Does a range check on age stop the user typing 15 when they are really 17?

Show answer

No. 15 is inside the range, so it passes. Validation checks that the value is reasonable, not that it is true.

Where this leads next

Validation loops are one of the most common places for termination mistakes, so continue to repairing a loop termination condition. Practise stepping the code in the restricted pseudocode trace trainer.

A teacher on our online one-to-one Computer Science tuition can give you fresh validation scenarios and check each condition you write.

Questions people ask

Does validation prove the data is correct?

No. Validation checks that data is reasonable and allowed, for example an age between 12 and 18. It cannot know whether the age is true. Verification, such as entering a value twice, is a separate idea that checks for entry errors.

Can I use a built-in function like isdigit in my answer?

A pseudocode answer should describe the check using the standard constructs: comparisons, loops and the functions the pseudocode guide lists. A language-specific method may not be recognised. Describe the check in steps instead, or check which functions the Cambridge guide includes.

Should the prompt be repeated inside the loop?

Yes. If the first input is invalid, the loop needs to ask again, so the INPUT line must appear inside the loop as well as before it. Otherwise the same bad value is tested forever.

Updated:

Your next step

If you can name each validation check but stumble when you have to write the loop, a one-to-one teacher can build the answer with you one condition at a time.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parents: enquire here

  • 9,000+ students helped through our service
  • 9+ years helping IGCSE students