Input validation with REPEAT…UNTIL

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Input validation with REPEAT…UNTIL

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop is most suitable for validating a Mark, because the prompt must run at least once?
A REPEAT...UNTIL
B FOR...NEXT
2. Which condition correctly accepts only marks from 0 to 100?
A UNTIL Mark >= 0 OR Mark <= 100
B UNTIL Mark >= 0 AND Mark <= 100
3. When does a REPEAT...UNTIL validation loop stop asking for input?
A When the UNTIL condition becomes TRUE
B When the UNTIL condition becomes FALSE
4. Where should the error message go in a validation loop?
A After ENDWHILE, once the loop has finished
B Inside the loop, shown when the input is invalid
5. A student types 150 for Mark. What happens with UNTIL Mark >= 0 AND Mark <= 100?
A The condition is FALSE, so the loop repeats and asks again
B The condition is TRUE, so the loop ends and 150 is accepted

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1UNTIL Mark >= 0 AND Mark <= 100 ARepeats the loop until the mark is in the valid range of 0 to 100 inclusive
2OUTPUT "Invalid mark, try again" BREPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
3REPEAT is suitable for validation because the prompt must run at least once before the check CThe UNTIL line is indented and sits like it is inside the loop body, and the error message is missing. UNTIL should line up with REPEAT, with the OUTPUT message placed before it inside the loop.
4An OR condition can be used to detect invalid data, so the error message appears when the mark is out of range DOR makes the condition true for every number, so the loop ends on any input and invalid marks are accepted. It should use AND.
5REPEAT INPUT Mark UNTIL Mark >= 0 OR Mark <= 100 EIF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
6REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 FShows an error message inside the loop when the value fails the range check

Definitions

UNTIL Mark >= 0 AND Mark <= 100

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
REPEAT INPUT Mark UNTIL Mark >= 0 ___ Mark <= 100
2.
REPEAT INPUT Mark _____ Mark >= 0 AND Mark <= 100
3.
______ INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
4.
REPEAT INPUT Mark UNTIL NOT (Mark < 0 __ Mark > 100)
5.
REPEAT _____ Mark UNTIL Mark >= 0 AND Mark <= 100

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A REPEAT
B INPUT Mark
C UNTIL Mark>=0 OR Mark<=100
D OUTPUT Mark
Question 2
A REPEAT
B OUTPUT Mark
C UNTIL Mark>=0 AND Mark<=100
D OUTPUT Mark
Question 3
A WHILE Mark<0 DO
B INPUT Mark
C UNTIL Mark>=0 AND Mark<=100
Question 4
A REPEAT
B INPUT Mark
C IF Mark<0 AND Mark>100 THEN
D OUTPUT "Invalid mark"
E ENDIF
F UNTIL Mark>=0 AND Mark<=100
Question 5
A REPEAT
B INPUT Mark
C IF Mark<0 OR Mark>100 THEN
D INPUT "Invalid mark"
E ENDIF
F UNTIL Mark>=0 AND Mark<=100

Answer Key

Input validation with REPEAT…UNTIL
Section A: Quick Questions
Q1
A — REPEAT...UNTIL
Q2
B — UNTIL Mark >= 0 AND Mark <= 100
Q3
A — When the UNTIL condition becomes TRUE
Q4
B — Inside the loop, shown when the input is invalid
Q5
A — The condition is FALSE, so the loop repeats and asks again
Section B: Match the Pairs
Q1
UNTIL Mark >= 0 AND Mark <= 100 → Repeats the loop until the mark is in the valid range of 0 to 100 inclusive
Q2
OUTPUT "Invalid mark, try again" → Shows an error message inside the loop when the value fails the range check
Q3
REPEAT is suitable for validation because the prompt must run at least once before the check → REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q4
An OR condition can be used to detect invalid data, so the error message appears when the mark is out of range → IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Q5
REPEAT INPUT Mark UNTIL Mark >= 0 OR Mark <= 100 → OR makes the condition true for every number, so the loop ends on any input and invalid marks are accepted. It should use AND.
Q6
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 → The UNTIL line is indented and sits like it is inside the loop body, and the error message is missing. UNTIL should line up with REPEAT, with the OUTPUT message placed before it inside the loop.
Section C: Definitions
Q1
UNTIL Mark >= 0 AND Mark <= 100 — Ends a REPEAT loop only when Mark is valid (0 to 100). Input is asked again until it passes this range check, so the program only continues with valid data.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q2
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q3
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q4
REPEAT INPUT Mark UNTIL NOT (Mark < 0 OR Mark > 100)
Q5
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Section E: Spot the Error
Q1
C — OR makes the condition true for every number, because any number is either at least 0 or at most 100. The loop would accept invalid marks. A range check needs AND: UNTIL Mark>=0 AND Mark<=100.
Q2
B — The loop must ask for a new value each time round, so line 2 should be INPUT Mark. With OUTPUT, no value is ever read and the user cannot correct a bad mark.
Q3
A — A loop that ends with UNTIL must start with REPEAT. WHILE...DO belongs with ENDWHILE. REPEAT also suits validation because the prompt must run at least once.
Q4
C — No number can be below 0 and above 100 at the same time, so the error message would never show. An invalid mark is below 0 OR above 100: IF Mark<0 OR Mark>100 THEN.
Q5
D — The error message must be shown to the user, so it needs OUTPUT, not INPUT. INPUT would try to read a value into a variable. It should be OUTPUT "Invalid mark".