Choosing a for loop or a while loop (JavaScript)
Use for when you know how many times to repeat, like 1 to 10. Use while when repeating depends on a condition, like asking until the answer is correct.
Understanding Choosing a for loop or a while loop (JavaScript) in ACARA Digital Technologies Years 7–8
What it does: The for loop's three parts (start, condition, update) are all still there. The while version just spreads them out, which is why it is often harder to read. When the number of repeats is known, a for loop keeps the start, stop and step together in one line. Use while when repeating depends on a condition, such as the guess being correct.
Common mistakes: n-- makes n smaller, so n <= 10 is always true and the loop never ends. A while loop must change its variable towards ending, so it should be n++;. The new answer is never stored in guess, so the condition never changes and the loop keeps asking forever. It should be guess = prompt("Again?");.
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
for vs whileFill in the Blanks
Spot the Error
Student with a class code? Join your class →