Validating input with a REPEAT loop

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

Validating input with a REPEAT loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop is the natural choice to keep asking for a mark until it is between 0 and 100?
A FOR Mark ← 0 TO 100 INPUT Mark NEXT Mark
B REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
2. Which condition correctly ends the loop once a valid mark (0 to 100) has been entered?
A UNTIL Mark >= 0 AND Mark <= 100
B UNTIL Mark >= 0 OR Mark <= 100
3. A student types 150 as a mark. What does REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 do?
A It accepts 150 and carries on
B It asks for INPUT again
4. Where should the error message go in a REPEAT validation loop?
A Inside the loop, in an IF after the INPUT
B After the UNTIL line, so it only shows once at the end
5. Which check does this loop perform? REPEAT INPUT Name UNTIL Name <> ""
A A presence check
B A range check

Match the Pairs

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

Left columnRight column
1REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 AKeeps asking for Age until it is in the range 0 to 120, so an invalid age never gets into the algorithm.
2REPEAT INPUT Name UNTIL LENGTH(Name) > 0 BKeeps asking for Name until something is typed in, which is a presence check.
3A validation loop that shows an error message each time the value is outside the range 1 to 10 CINPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
4The WHILE form of a range check: read first, then keep asking while the value is invalid DOR makes the condition true for every number, so the range check never rejects anything. It should use AND.
5REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100 EREPEAT INPUT Score IF Score < 1 OR Score > 10 THEN OUTPUT "Enter 1 to 10" ENDIF UNTIL Score >= 1 AND Score <= 10
6REPEAT INPUT Mark UNTIL Mark >= 0 OR Mark <= 100 FThe UNTIL condition tests for invalid values, so the loop stops as soon as the mark is wrong. It should be UNTIL Mark >= 0 AND Mark <= 100.

Definitions

REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100

Fill in the Blanks

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

1.
______ INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
2.
REPEAT INPUT Password UNTIL ______(Password) >= 8
3.
IF Mark < 0 OR Mark > 100 THEN ______ "Invalid mark" ENDIF
4.
WHILE Mark < 0 __ Mark > 100 DO INPUT Mark ENDWHILE
5.
REPEAT INPUT 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 Num
C UNTIL Num >= 1 OR Num <= 10
D OUTPUT Num
Question 2
A REPEAT
B OUTPUT Age
C UNTIL Age >= 5 AND Age <= 18
D OUTPUT Age
Question 3
A INPUT Num
B WHILE Num < 1 AND Num > 10 DO
C OUTPUT "Enter 1 to 10"
D INPUT Num
E ENDWHILE
Question 4
A WHILE
B INPUT Num
C UNTIL Num >= 1 AND Num <= 10
Question 5
A DECLARE Num : STRING
B REPEAT
C INPUT Num
D UNTIL Num >= 1 AND Num <= 10

Answer Key

Validating input with a REPEAT loop
Section A: Quick Questions
Q1
B — REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q2
A — UNTIL Mark >= 0 AND Mark <= 100
Q3
B — It asks for INPUT again
Q4
A — Inside the loop, in an IF after the INPUT
Q5
A — A presence check
Section B: Match the Pairs
Q1
REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 → Keeps asking for Age until it is in the range 0 to 120, so an invalid age never gets into the algorithm.
Q2
REPEAT INPUT Name UNTIL LENGTH(Name) > 0 → Keeps asking for Name until something is typed in, which is a presence check.
Q3
A validation loop that shows an error message each time the value is outside the range 1 to 10 → REPEAT INPUT Score IF Score < 1 OR Score > 10 THEN OUTPUT "Enter 1 to 10" ENDIF UNTIL Score >= 1 AND Score <= 10
Q4
The WHILE form of a range check: read first, then keep asking while the value is invalid → INPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Q5
REPEAT INPUT Mark UNTIL Mark < 0 OR Mark > 100 → The UNTIL condition tests for invalid values, so the loop stops as soon as the mark is wrong. It should be UNTIL Mark >= 0 AND Mark <= 100.
Q6
REPEAT INPUT Mark UNTIL Mark >= 0 OR Mark <= 100 → OR makes the condition true for every number, so the range check never rejects anything. It should use AND.
Section C: Definitions
Q1
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100 — A validation loop: it asks for INPUT again and again until the value passes the check (here a range check), so invalid data never enters the algorithm.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q2
REPEAT INPUT Password UNTIL LENGTH(Password) >= 8
Q3
IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Q4
WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Q5
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Section E: Spot the Error
Q1
C — With OR the condition is true for every number, so the loop stops straight away and invalid values get through. A range check needs AND: UNTIL Num >= 1 AND Num <= 10.
Q2
B — The loop body must ask for the value again with INPUT. OUTPUT never reads a new Age, so the check cannot be passed. Line 2 should be INPUT Age.
Q3
B — No number can be below 1 and above 10 at once, so the loop never runs and invalid data is accepted. The WHILE form repeats while the value is invalid, so it needs OR: WHILE Num < 1 OR Num > 10 DO.
Q4
A — The loop ends with UNTIL, so it must begin with REPEAT. WHILE needs DO and ENDWHILE instead. Line 1 should be REPEAT.
Q5
A — A range check compares Num with numbers, so Num must be a numeric type. It should be declared as INTEGER, not STRING.