← ACARA Digital Technologies Years 7–8

Repeating until valid input is given

A loop that re-asks with prompt() while the answer fails the check, showing an error message each time, so bad input never continues. Here it repeats while num is outside 1 to 10.

Understanding Repeating until valid input is given in ACARA Digital Technologies Years 7–8

What it does: The first prompt() happens before the while loop. This gives num a value so the loop condition has something to test. This line goes inside the loop body, so it runs every time the answer is out of range. The user is told why they are being asked again. 15 is greater than 10, so num > 10 is true. The || makes the whole condition true, so the loop re-asks. No number can be below 1 AND above 10 at once, so && is never true and bad input would get through. Use || for 'either too low or too high'.

Common mistakes: == compares instead of assigning, so the new answer is never saved in n. The condition stays true and the loop never ends. It should be n = Number(prompt("N?"));. No number is below 1 and above 120 at the same time, so && is never true. The loop is skipped and bad ages get through. It should use || to catch either bad case.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 599–610) and word search (Modules 583–613).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which loop keeps asking until the number is between 1 and 10?
while (num < 1 || num > 10) { num = Number(prompt("1-10?")); }while (num >= 1 && num <= 10) { num = Number(prompt("1-10?")); }
Matches
while (num >= 1 || num <= 10) {
↓
The condition checks for valid numbers, not invalid ones. Almost every number is >= 1 or <= 10, so the loop keeps repeating even for good answers.
Applying
Meanings
Term
while (!(num >= 1 && num <= 10))
Meaning
A loop that re-asks with prompt() while the answer fails the check, showing an error message each time, so bad input never continues. Here it repeats while num is outside 1 to 10.
Practice
let num = Number(prompt("Pick 1 to 10:"));
Testing
Gaps
(n < 1 || n > 10) {
Check Answer
Spots
3 console.log("Try again");
4 n == Number(prompt("N?"));

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Reef Shark' badge — Reef Ruler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which loop keeps asking until the number is between 1 and 10?
A. while (num < 1 || num > 10) { num = Number(prompt("1-10?")); } ✓
B. while (num >= 1 && num <= 10) { num = Number(prompt("1-10?")); }
The loop condition describes BAD input (out of range), so it keeps asking while the answer is invalid. It stops once the number is 1 to 10.
2. Where should the first prompt() go in a validation loop that checks the answer?
A. Nowhere, the loop can check num before it has a value
B. Before the loop, so the condition has a value to check ✓
Ask once before the loop so num holds the student's first answer. The loop then checks it and re-asks inside if it is bad.
3. Where must the prompt() go so the student is asked again after a bad answer?
A. Only before the loop, so it runs once
B. Inside the loop body, so num is updated each time round ✓
If prompt() is only before the loop, num never changes inside it. A bad answer would then repeat forever (an infinite loop).
4. Which line inside the loop gives the student an error message each time they type a bad answer?
A. alert("Please enter a number from 1 to 10"); ✓
B. alert = "Please enter a number from 1 to 10";
alert() is a function call, so it needs brackets. Putting it inside the loop shows the message every time the answer fails the check.
5. A student types 15 when the program asks for 1 to 10. What happens with this loop: while (num < 1 || num > 10)?
A. The condition is true, so the loop runs and asks again ✓
B. The condition is false, so the program carries on
15 is greater than 10, so num > 10 is true. The || makes the whole condition true, so the loop re-asks.

Match the Pairs

while (num < 1 || num > 10) { → Keeps looping while the number is outside the range 1 to 10, so the loop body runs only for invalid answers.
num = Number(prompt("Pick 1-10:")); → Asks the user again inside the loop and saves the new answer, so the condition can be checked again.
Ask once before the loop so the variable has a value for the first check → let num = Number(prompt("Pick 1-10:"));
Show an error message each time the answer fails the range check → console.log("Invalid! Try again.");
while (num >= 1 || num <= 10) { → The condition checks for valid numbers, not invalid ones. Almost every number is >= 1 or <= 10, so the loop keeps repeating even for good answers.
while (num < 1 || num > 10) { console.log("Out of range!"); } → There is no prompt() inside the loop, so num never changes. The condition stays true and the loop runs forever.

Key Term

while (!(num >= 1 && num <= 10))
A loop that re-asks with prompt() while the answer fails the check, showing an error message each time, so bad input never continues. Here it repeats while num is outside 1 to 10.

Fill in the Blanks

1. while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Hint: Type the keyword that keeps repeating the block while the answer is out of range
2. while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Hint: Type the logical operator that makes the loop run if either range check is true
3. while (n < 1 || n > 10) { n = Number(prompt("Try again:")); }
Hint: Type the function that asks the user for a new answer inside the loop
4. console.log("Out of range!"); n = Number(prompt("Try again:")); }
Hint: Type the command that shows an error message each time the answer is bad
5. let n = Number(prompt("Pick 1-10:")); while (n < 1 || n > 10) { n = Number(prompt("Again:")); }
Hint: Type the function that turns the typed text into a number before the first range check

Spot the Error

Question 1
A let n = Number(prompt("N?"));
B while (n < 1 || n > 10) {
C console.log("Try again");
D n == Number(prompt("N?"));
E }
Line D — == compares instead of assigning, so the new answer is never saved in n. The condition stays true and the loop never ends. It should be n = Number(prompt("N?"));
Question 2
A let a = Number(prompt("Age"));
B while (a < 1 && a > 120) {
C console.log("Bad age");
D a = Number(prompt("Age"));
E }
Line B — No number is below 1 and above 120 at the same time, so && is never true. The loop is skipped and bad ages get through. It should use || to catch either bad case.
Question 3
A let n = Number(prompt("N?"));
B if (n < 1 || n > 10) {
C alert("Enter 1 to 10");
D n = Number(prompt("N?"));
E }
Line B — An if only checks once, so the second answer might still be bad. It should be while so the program keeps asking until the answer is valid.
Question 4
A let p = Number(prompt("P"));
B while (p < 0 || p > 50) {
C console.log("Use 0 to 50");
D let p = Number(prompt("P"));
E }
Line D — let makes a new p that only exists inside the loop. The p in the while condition never changes, so the loop never ends. It should be p = Number(prompt("P"));
Question 5
A let n;
B while (n < 1 || n > 10) {
C console.log("Out of range");
D n = Number(prompt("N?"));
E }
Line A — The program must ask once before the loop. Without it n is undefined, the comparisons are false and the loop is skipped. It should be let n = Number(prompt("N?"));

Student with a class code? Join your class →