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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
while (!(num >= 1 && num <= 10))Fill in the Blanks
Spot the Error
Student with a class code? Join your class →