← ACARA Digital Technologies Years 7–8

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?");.

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
You want to print the numbers 1 to 10. Which loop is the best choice?
while loop, because it can never be used for countingfor loop, because you know it repeats 10 times
Matches
let guess = prompt("Number?"); for (let i = 0; guess !== "7"; ) { guess = prompt("Number?"); }
↓
A for loop is a poor fit here because the number of repeats is unknown and the counter i is never used. A while loop (while (guess !== "7")) is clearer.
Applying
Meanings
Term
for vs while
Meaning
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.
Practice
for (let i = 1; i <= 3; i++) {
Testing
Gaps
(let i = 1; i <= 10; i++) {
Check Answer
Spots
3 console.log(n);
4 n--;

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

The 'Parrotfish' badge — Function Builder, 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. You want to print the numbers 1 to 10. Which loop is the best choice?
A. for loop, because you know it repeats 10 times ✓
B. while loop, because it can never be used for counting
When the number of repeats is known, a for loop keeps the start, stop and step together in one line.
2. A program keeps asking 'What is the password?' until the user types the right answer. Which loop fits best?
A. while (answer !== password) { ... } ✓
B. for (let i = 0; i < 10; i++) { ... }
You do not know how many tries the user needs, so the loop depends on a condition. That is a job for while.
3. True or false: Any for loop can be rewritten as a while loop.
A. False
B. True ✓
Put the setup before the loop, the condition in the brackets, and the update inside the loop body. The result is the same.
4. Which while loop does the same job as: for (let i = 1; i <= 5; i++) { console.log(i); }
A. let i = 1; while (i <= 5) { console.log(i); i++; } ✓
B. let i = 1; while (i <= 5) { console.log(i); }
The while version still needs i++ inside the loop. Without it, i never changes and the loop runs forever.
5. A game repeats a round exactly 3 times. Which loop is easier to read?
A. while (true) with a counter hidden further down
B. for (let round = 1; round <= 3; round++) ✓
A for loop shows the start, the end and the step in one line, so it is easier to read when the count is known.

Match the Pairs

for (let i = 1; i <= 10; i++) { → A for loop is a good choice here because we know exactly how many times to repeat: 10 times.
while (answer !== "blue") { → A while loop is a good choice here because we repeat until a condition changes, and we do not know how many tries it will take.
Any for loop can be rewritten as a while loop by moving the setup before the loop and the update inside it. → let i = 1; while (i <= 10) { console.log(i); i++; }
Use a for loop for the readable choice when printing the numbers 1 to 10, because the counting is all in one place. → for (let i = 1; i <= 10; i++) { console.log(i); }
let guess = prompt("Number?"); for (let i = 0; guess !== "7"; ) { guess = prompt("Number?"); } → A for loop is a poor fit here because the number of repeats is unknown and the counter i is never used. A while loop (while (guess !== "7")) is clearer.
let i = 1; while (i <= 10) { console.log(i); } → The loop never updates i, so it runs forever. Add i++ inside the loop, or use a for loop for a known count of 10.

Key Term

for vs while
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.

Fill in the Blanks

1. for (let i = 1; i <= 10; i++) { console.log(i); }
Hint: Type the loop keyword that suits repeating a known number of times, such as printing 1 to 10.
2. let ans = ""; while (ans !== "yes") { ans = prompt("?"); }
Hint: Type the loop keyword used when repeating depends on a condition, like asking until the answer is correct.
3. let i = 1; while (i <= 10) { console.log(i); i++; }
Hint: This for loop is written as a while loop. Type the update that moves the counter on so the loop can end.
4. for (let i = 1; i <= 5; i++) { console.log("Hi"); }
Hint: Type the comparison operator that keeps the loop running up to and including the last number.
5. let total = 0; for (let n = 1; n <= 5; n++) { total += n; }
Hint: Type the final counter value. The for header shows start, end and step in one tidy line.

Spot the Error

Question 1
A let n = 1;
B while (n <= 10) {
C console.log(n);
D n--;
E }
Line D — 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++;.
Question 2
A let guess = prompt("Guess?");
B while (guess !== "7") {
C prompt("Again?");
D }
E console.log("Correct!");
Line C — 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?");.
Question 3
A let count = 0;
B for (let i = 0; i < 10; i++) {
C count + 1;
D }
E console.log(count);
Line C — count + 1; works out a value but never saves it, so count stays 0. It should be count = count + 1; (or count++;).
Question 4
A let total = 0;
B let n = 5;
C for (let i = 1, i <= n, i++) {
D total += i;
E }
Line C — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i = 1; i <= n; i++) {.
Question 5
A let answer = "";
B while (answer = "yes") {
C answer = prompt("Ready?");
D }
Line B — A single = assigns a value instead of comparing. The condition should compare with !==, like while (answer !== "yes") {, so the loop keeps asking until the answer is yes.

Student with a class code? Join your class →