← ACARA Digital Technologies Years 7–8

Infinite loops and updating the loop variable

A loop runs forever if nothing in its body changes the variable its condition tests. Updating count each time lets the condition become false so the loop stops.

Understanding Infinite loops and updating the loop variable in ACARA Digital Technologies Years 7–8

What it does: lives-- lowers lives each time round, so lives > 0 eventually becomes false. n only gets bigger, so n > 0 is always true and the loop never ends. The loop needs n to move toward 0. Using n-- would make n > 0 become false eventually. count++ updates the variable the condition tests, so the loop reaches an end.

Common mistakes: i-- makes i smaller each time, so it never reaches 5 and the loop never stops. It should be i++ so i counts up to 5. count only goes up, so count >= 0 is always true and the condition can never become false. It should be count < 5.

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
let count = 0; while (count < 5) { console.log(count); } What happens when this loop runs?
It prints 0 to 4 and then stopsIt never stops, because count is never changed
Matches
let count = 0; while (count < 5) { console.log(count); }
↓
The loop never changes count, so count < 5 stays true forever. Add count++; inside the loop body.
Applying
Meanings
Term
while (count < 5) { count++; }
Meaning
A loop runs forever if nothing in its body changes the variable its condition tests. Updating count each time lets the condition become false so the loop stops.
Practice
let count = 0;
Testing
Gaps
while (count < 5) { ; }
Check Answer
Spots
1for (let i = 0; i < 5; i--) {
2 console.log(i);

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

The 'Space Turtle' badge — Speed in Zero-G, 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. let count = 0; while (count < 5) { console.log(count); } What happens when this loop runs?
A. It prints 0 to 4 and then stops
B. It never stops, because count is never changed ✓
Nothing in the loop body changes count, so count < 5 is always true and the loop runs forever. Adding count++ fixes it.
2. Which loop body lets the condition count < 5 eventually become false?
A. console.log(count); count++; ✓
B. console.log(count); console.log(5);
count++ adds 1 to count each time round. Once count reaches 5, count < 5 is false and the loop stops.
3. True or false: A while loop stops by itself after a few turns, even if its variable never changes.
A. False ✓
B. True
A while loop only stops when its condition becomes false. If the variable in the condition never changes, the loop keeps going.
4. Which loop is an infinite loop?
A. while (n < 3) { n++; }
B. while (true) { console.log("Hi"); } ✓
The condition true can never become false, so nothing can end the loop. The other loop updates n, so it finishes.
5. let n = 10; while (n > 0) { n++; } Why does this loop never stop?
A. n++ makes n bigger, so n > 0 stays true ✓
B. n starts at 10, so the loop is skipped
The loop needs n to move toward 0. Using n-- would make n > 0 become false eventually.

Match the Pairs

while (count < 5) { count++; } → The loop stops because count++ changes count each time, so count < 5 eventually becomes false.
while (true) { console.log("Hi"); } → An infinite loop: the condition is always true and nothing in the body can make it false.
To make a loop stop, update the variable that the condition tests inside the loop body. → let lives = 3; while (lives > 0) { lives--; }
A condition that can never become false makes the loop run forever, even if the variable is updated. → let n = 1; while (n > 0) { n++; }
let count = 0; while (count < 5) { console.log(count); } → The loop never changes count, so count < 5 stays true forever. Add count++; inside the loop body.
let count = 10; while (count > 0) { count++; } → count goes up, moving away from 0, so count > 0 is always true. Use count-- so it counts down to 0.

Key Term

while (count < 5) { count++; }
A loop runs forever if nothing in its body changes the variable its condition tests. Updating count each time lets the condition become false so the loop stops.

Fill in the Blanks

1. while (count < 5) { count++; }
Hint: Type the statement in the body that changes the variable the condition tests, so the loop can end.
2. let lives = 3; while (lives > 0) { lives--; }
Hint: Type the operator that moves the number towards making the condition false.
3. while (true) { console.log("Hi"); }
Hint: Type the boolean value that means the condition can never become false.
4. let n = 0; while (n < 3) { n++; }
Hint: Type the variable name that the condition tests and the loop body updates.
5. while (true) { if (n > 4) break; n++; }
Hint: Type the keyword that stops a running loop immediately from inside it.

Spot the Error

Question 1
A for (let i = 0; i < 5; i--) {
B console.log(i);
C }
Line A — i-- makes i smaller each time, so it never reaches 5 and the loop never stops. It should be i++ so i counts up to 5.
Question 2
A let count = 0;
B while (count >= 0) {
C count++;
D }
Line B — count only goes up, so count >= 0 is always true and the condition can never become false. It should be count < 5.
Question 3
A let n = 10;
B while (n < 20) {
C n--;
D }
Line B — n goes down from 10, so it is always less than 20 and the condition never becomes false. It should be n > 0 so the loop ends when n reaches 0.
Question 4
A let count = 0;
B while (count < 3) {
C console.log("Hi");
D count--;
E }
Line D — count-- moves count away from 3, so count < 3 stays true forever. The loop variable must move towards the stopping value, so use count++.
Question 5
A for (let i = 5; i > 0; ) {
B console.log(i);
C }
Line A — The update part has been left out, so i stays at 5 and i > 0 is always true. It should end with i-- to count down.

Student with a class code? Join your class →