Re-asking for input until it is valid

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

Re-asking for input until it is valid

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student wants a program to keep asking for a mark until it is between 0 and 100. Which loop condition is correct?
A REPEAT ... UNTIL Mark >= 0 AND Mark <= 100
B REPEAT ... UNTIL Mark < 0 OR Mark > 100
2. Where should the INPUT command go in a validation loop built with REPEAT ... UNTIL?
A Inside the loop, so a new value is read every time
B Before the loop only, so it is read once
3. Which statement about REPEAT ... UNTIL is true?
A The condition is checked first, so the body might never run
B The body always runs at least once, so the user is always asked once
4. Where does the error message go in a REPEAT ... UNTIL validation loop?
A After the loop, so it is shown once the value is valid
B Inside the loop in an IF that runs when the value is invalid
5. Which UNTIL condition ends the loop for a Age that must be 11 to 18?
A UNTIL Age >= 11 AND Age <= 18
B UNTIL Age < 11 OR Age > 18

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 AREPEAT OUTPUT "Enter 1 to 10" INPUT Num UNTIL Num >= 1 AND Num <= 10
2IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF BINPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
3A WHILE version asks for the first input before the loop, then repeats the INPUT while the value is invalid CThere is no INPUT before the WHILE loop, so Num has no value to check the first time. Add INPUT Num before the loop.
4A REPEAT loop always runs the prompt and INPUT at least once, and UNTIL checks the value is valid DThe UNTIL condition describes invalid data, so the loop stops on an invalid age and keeps asking for valid ones. It should be UNTIL Age >= 0 AND Age <= 120.
5REPEAT INPUT Age UNTIL Age < 0 OR Age > 120 EKeeps asking for Age until the value is inside the valid range 0 to 120, then the loop ends
6WHILE Num < 1 OR Num > 10 DO INPUT Num ENDWHILE FShows an error message inside the loop when the value fails the validation check

Definitions

REPEAT INPUT UNTIL valid

Fill in the Blanks

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

1.
REPEAT _____ Age UNTIL Age >= 1 AND Age <= 100
2.
REPEAT INPUT Mark _____ Mark >= 0 AND Mark <= 100
3.
REPEAT INPUT Mark UNTIL Mark >= 0 ___ Mark <= 100
4.
WHILE Mark < 0 __ Mark > 100 DO INPUT Mark ENDWHILE
5.
IF Mark < 0 OR Mark > 100 THEN ______ "Invalid mark" ENDIF

Spot the Error

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

Question 1
A REPEAT
B INPUT Num
C IF Num < 1 AND Num > 10 THEN
D OUTPUT "Error"
E ENDIF
F UNTIL Num >= 1 AND Num <= 10
Question 2
A INPUT Pts
B WHILE Pts < 0 OR Pts > 50 DO
C OUTPUT "Invalid"
D OUTPUT Pts
E ENDWHILE
Question 3
A REPEAT
B INPUT Points
C IF Pts < 0 OR Pts > 50 THEN
D OUTPUT "Out of range"
E ENDIF
F UNTIL Pts >= 0 AND Pts <= 50
Question 4
A REPEAT
B INPUT Num
C UNTIL Num < 1 OR Num > 5
D OUTPUT "Accepted"
Question 5
A WHILE
B INPUT Qty
C IF Qty < 1 OR Qty > 20 THEN
D OUTPUT "Enter 1 to 20"
E ENDIF
F UNTIL Qty >= 1 AND Qty <= 20

Answer Key

Re-asking for input until it is valid
Section A: Quick Questions
Q1
A — REPEAT ... UNTIL Mark >= 0 AND Mark <= 100
Q2
A — Inside the loop, so a new value is read every time
Q3
B — The body always runs at least once, so the user is always asked once
Q4
B — Inside the loop in an IF that runs when the value is invalid
Q5
A — UNTIL Age >= 11 AND Age <= 18
Section B: Match the Pairs
Q1
REPEAT INPUT Age UNTIL Age >= 0 AND Age <= 120 → Keeps asking for Age until the value is inside the valid range 0 to 120, then the loop ends
Q2
IF Age < 0 OR Age > 120 THEN OUTPUT "Invalid age" ENDIF → Shows an error message inside the loop when the value fails the validation check
Q3
A WHILE version asks for the first input before the loop, then repeats the INPUT while the value is invalid → INPUT Mark WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Q4
A REPEAT loop always runs the prompt and INPUT at least once, and UNTIL checks the value is valid → REPEAT OUTPUT "Enter 1 to 10" INPUT Num UNTIL Num >= 1 AND Num <= 10
Q5
REPEAT INPUT Age UNTIL Age < 0 OR Age > 120 → The UNTIL condition describes invalid data, so the loop stops on an invalid age and keeps asking for valid ones. It should be UNTIL Age >= 0 AND Age <= 120.
Q6
WHILE Num < 1 OR Num > 10 DO INPUT Num ENDWHILE → There is no INPUT before the WHILE loop, so Num has no value to check the first time. Add INPUT Num before the loop.
Section C: Definitions
Q1
REPEAT INPUT UNTIL valid — A REPEAT loop around INPUT that keeps asking for a value and shows an error message each time the check fails. The UNTIL condition is the valid range, so the loop ends only on valid data.
Section D: Fill in the Blanks
Q1
REPEAT INPUT Age UNTIL Age >= 1 AND Age <= 100
Q2
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q3
REPEAT INPUT Mark UNTIL Mark >= 0 AND Mark <= 100
Q4
WHILE Mark < 0 OR Mark > 100 DO INPUT Mark ENDWHILE
Q5
IF Mark < 0 OR Mark > 100 THEN OUTPUT "Invalid mark" ENDIF
Section E: Spot the Error
Q1
C — A number cannot be below 1 and above 10 at the same time, so the error message would never show. Invalid data is below the range OR above it: IF Num < 1 OR Num > 10 THEN.
Q2
D — Inside the WHILE loop the program must ask again with INPUT Pts. Outputting the old value never changes Pts, so the loop would never end.
Q3
B — The value is read into a different variable (Points) from the one the check and UNTIL use (Pts). It should be INPUT Pts.
Q4
C — The UNTIL condition must describe VALID data, so the loop stops only when the value is in range. This version stops on invalid data. It should be UNTIL Num >= 1 AND Num <= 5.
Q5
A — The loop ends with UNTIL, so it must start with REPEAT. WHILE needs a condition and DO, and finishes with ENDWHILE.