Infinite loops and updating the loop variable

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Infinite loops and updating the loop variable

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
2. Which loop body lets the condition count < 5 eventually become false?
A console.log(count); count++;
B console.log(count); console.log(5);
3. True or false: A while loop stops by itself after a few turns, even if its variable never changes.
A False
B True
4. Which loop is an infinite loop?
A while (n < 3) { n++; }
B while (true) { console.log("Hi"); }
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

while (count < 5) { count++; }

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
while (count < 5) { _______; }
2.
let lives = 3; while (lives > 0) { lives__; }
3.
while (____) { console.log("Hi"); }
4.
let n = 0; while (_ < 3) { n++; }
5.
while (true) { if (n > 4) _____; n++; }

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A for (let i = 0; i < 5; i--) {
B console.log(i);
C }
Question 2
A let count = 0;
B while (count >= 0) {
C count++;
D }
Question 3
A let n = 10;
B while (n < 20) {
C n--;
D }
Question 4
A let count = 0;
B while (count < 3) {
C console.log("Hi");
D count--;
E }
Question 5
A for (let i = 5; i > 0; ) {
B console.log(i);
C }

Answer Key

Infinite loops and updating the loop variable
Section A: Quick Questions
Q1
B — It never stops, because count is never changed
Q2
A — console.log(count); count++;
Q3
A — False
Q4
B — while (true) { console.log("Hi"); }
Q5
A — n++ makes n bigger, so n > 0 stays true
Section B: Match the Pairs
Q1
while (count < 5) { count++; } → The loop stops because count++ changes count each time, so count < 5 eventually becomes false.
Q2
while (true) { console.log("Hi"); } → An infinite loop: the condition is always true and nothing in the body can make it false.
Q3
To make a loop stop, update the variable that the condition tests inside the loop body. → let lives = 3; while (lives > 0) { lives--; }
Q4
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++; }
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
while (count < 5) { count++; }
Q2
let lives = 3; while (lives > 0) { lives--; }
Q3
while (true) { console.log("Hi"); }
Q4
let n = 0; while (n < 3) { n++; }
Q5
while (true) { if (n > 4) break; n++; }
Section E: Spot the Error
Q1
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.
Q2
B — count only goes up, so count >= 0 is always true and the condition can never become false. It should be count < 5.
Q3
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.
Q4
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++.
Q5
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.