Validation and test data
Check the data before you use it
- Users make mistakes: a mark of 120, an empty name, a password that is too short.
- Validation is a check the program makes automatically: is this value reasonable? It cannot tell whether the value is true, only whether it is allowed.
- The syllabus (section 7) names six checks: range, length, type, presence, format and check digit.
- Verification checks that data was entered as intended: double entry (type it twice and compare) or a visual check (read it back).
Ask again until the input is valid
- Put the
INPUTinside aREPEAT ... UNTILloop. The loop runs at least once, then stops only when the value passes the check. - Output an error message inside the loop, so the user knows what to fix.

DECLARE Attempts : ARRAY[1:3] OF INTEGER
DECLARE Position, Mark : INTEGER
Attempts[1] ← 120
Attempts[2] ← -5
Attempts[3] ← 64
Position ← 0
REPEAT
Position ← Position + 1
Mark ← Attempts[Position]
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "Error: enter a mark from 0 to 100"
ENDIF
UNTIL Mark >= 0 AND Mark <= 100
OUTPUT "Accepted: ", Mark
- This course has no keyboard here, so the array
Attemptsstands in for three things typed by a user. In an exam answer, writeINPUT Markin place ofMark ← Attempts[Position]. - 120 and -5 fail the range check, so each prints an error. 64 passes, and the loop ends.
Length, presence and type checks
| Check | Question it asks | Example in pseudocode |
|---|---|---|
| range | is it between two limits? | Mark >= 0 AND Mark <= 100 |
| length | does it have enough (or too many) characters? | LENGTH(Password) >= 8 |
| presence | was anything entered? | UserName <> "" |
| type | is it the right kind of data, such as a whole number? | Quantity = ROUND(Quantity, 0) |
| format | does it follow a pattern, such as DD/MM/YYYY? |
check each character's position |
| check digit | does the last digit match a calculation on the others? | used on ISBNs and bar codes |
DECLARE UserName, Password : STRING
DECLARE Quantity : REAL
UserName ← ""
Password ← "cat123"
Quantity ← 2.5
IF UserName = ""
THEN
OUTPUT "Presence check failed: enter a name"
ENDIF
IF LENGTH(Password) < 8
THEN
OUTPUT "Length check failed: use at least 8 characters"
ENDIF
IF Quantity <> ROUND(Quantity, 0)
THEN
OUTPUT "Type check failed: enter a whole number"
ENDIF
Choose test data
- To test a program you choose input values and write down the result you expect before you run it.
- For a mark that must be 0 to 100:
| Type | What it is | Examples | Expected result |
|---|---|---|---|
| normal | ordinary data that should be accepted | 50, 72 |
accepted |
| abnormal | data that should be rejected | -20, "ten" |
rejected |
| extreme | the largest and smallest values that are accepted | 0, 100 |
accepted |
| boundary | the values on each side of a limit | 0 and -1, 100 and 101 |
0, 100 accepted; -1, 101 rejected |

Common mistakes
| Wrong | Right | Why |
|---|---|---|
IF around a single INPUT |
REPEAT ... UNTIL around the INPUT |
an IF checks once; the user may type a wrong value again |
UNTIL Mark > 0 AND Mark < 100 |
UNTIL Mark >= 0 AND Mark <= 100 |
0 and 100 are allowed, so the test must include them |
UNTIL Mark >= 0 OR Mark <= 100 |
use AND |
with OR, every number passes |
| "validation checks the data is correct" | "validation checks the data is reasonable" | a valid age of 15 can still be the wrong age |
boundary data 100 only |
100 and 101 |
boundary data tests both sides of the limit |
Now you try
- Write the expected result for each test value first, then check your program gives it.
Independent transfer
The final task validates a password with a loop. Plan the check and the messages, then solve it before opening support. Test a valid password on the first try as well.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The array Attempts stands in for three ages typed by a user: 9, 25, 14. Use REPEAT ... UNTIL to take the next attempt as Age. Output Error for each age that is not 11 to 18. When an age is valid, output Accepted: and the age.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A quantity must be from 1 to 50. Give the four biên giới values in ascending order, separated by spaces.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read passwords until one has at least 8 characters. For each shorter one, output Too short. Then output Accepted after N tries, where N counts every password read.
Nhấn Chạy để xem kết quả ở đây.