A loop that never stops

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

A loop that never stops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A loop that never stops is called an infinite loop. What makes a loop never stop?
A It has too many steps inside it
B Its stopping condition can never become true
2. Which algorithm has a loop that never stops? SET count TO 1 REPEAT UNTIL count = 5 SAY "Hi"
A It never changes count, so count stays at 1 forever
B It stops after it says Hi five times
3. Which line, placed inside the loop, fixes the loop that never stops? SET count TO 1 REPEAT UNTIL count = 5 SAY "Hi"
A SAY "Stop"
B ADD 1 TO count
4. A loop says REPEAT UNTIL lives = 0. Inside the loop, lives goes UP by 1 each time. What happens?
A The loop never stops, because lives moves away from 0
B The loop stops quickly, because lives is changing
5. Which loop will stop?
A SET n TO 10, then REPEAT UNTIL n = 0: SUBTRACT 1 FROM n
B SET n TO 10, then REPEAT UNTIL n = 0: ADD 1 TO n

Match the Pairs

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

Left columnRight column
1REPEAT UNTIL count = 5 AAn infinite loop, because count never changes and so never reaches 5
2REPEAT UNTIL count = 5 (no change to count) BREPEAT UNTIL lives = 0: SET lives TO lives - 1
3Adding 1 to the counter inside the loop works towards the stopping condition CThe loop stops only when count becomes 5, so count must change inside the loop
4A loop that fixes the problem has a step that moves the variable towards the stop value DSET count TO count + 1
5SET count TO 0 REPEAT UNTIL count = 3 SAY "Hi" EThe counter is never changed inside the loop, so count stays 0 and the loop never stops. Add SET count TO count + 1 inside the loop.
6SET steps TO 10 REPEAT UNTIL steps = 0 SET steps TO steps + 1 FThe variable changes in the wrong direction. Steps goes up away from 0, so the loop never stops. It should be steps - 1.

Definitions

infinite loop

Fill in the Blanks

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

1.
SET count TO 0 REPEAT UNTIL count = 5 SET _____ TO count + 1
2.
SET lives TO 3 REPEAT _____ lives = 0 SET lives TO lives - 1
3.
SET count TO 1 REPEAT UNTIL count = 10 SET count TO count _ 1
4.
SET steps TO 0 REPEAT UNTIL _____ = 4 SET steps TO steps + 1
5.
SET water TO 5 REPEAT UNTIL water = 0 SET water TO water _ 1

Spot the Error

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

Question 1
A SET count TO 1
B REPEAT UNTIL count = 0
C SAY count
D SET count TO count + 1
Question 2
A SET lives TO 3
B REPEAT UNTIL lives = 5
C SET lives TO lives - 1
D SAY "Ouch!"
Question 3
A SET steps TO 1
B REPEAT UNTIL steps = 10
C SET steps TO steps + 2
D SAY steps
Question 4
A SET score TO 0
B REPEAT UNTIL score >= 20
C SET score TO score - 5
D SAY score
Question 5
A SET water TO 5
B SAY "Emptying bucket"
C REPEAT UNTIL water = 0
D SET water TO water
E SAY water

Answer Key

A loop that never stops
Section A: Quick Questions
Q1
B — Its stopping condition can never become true
Q2
A — It never changes count, so count stays at 1 forever
Q3
B — ADD 1 TO count
Q4
A — The loop never stops, because lives moves away from 0
Q5
A — SET n TO 10, then REPEAT UNTIL n = 0: SUBTRACT 1 FROM n
Section B: Match the Pairs
Q1
REPEAT UNTIL count = 5 → The loop stops only when count becomes 5, so count must change inside the loop
Q2
REPEAT UNTIL count = 5 (no change to count) → An infinite loop, because count never changes and so never reaches 5
Q3
Adding 1 to the counter inside the loop works towards the stopping condition → SET count TO count + 1
Q4
A loop that fixes the problem has a step that moves the variable towards the stop value → REPEAT UNTIL lives = 0: SET lives TO lives - 1
Q5
SET count TO 0 REPEAT UNTIL count = 3 SAY "Hi" → The counter is never changed inside the loop, so count stays 0 and the loop never stops. Add SET count TO count + 1 inside the loop.
Q6
SET steps TO 10 REPEAT UNTIL steps = 0 SET steps TO steps + 1 → The variable changes in the wrong direction. Steps goes up away from 0, so the loop never stops. It should be steps - 1.
Section C: Definitions
Q1
infinite loop — A loop that never stops because its stopping condition can never become true. Something inside the loop, like a counter, must change to work towards the condition.
Section D: Fill in the Blanks
Q1
SET count TO 0 REPEAT UNTIL count = 5 SET count TO count + 1
Q2
SET lives TO 3 REPEAT UNTIL lives = 0 SET lives TO lives - 1
Q3
SET count TO 1 REPEAT UNTIL count = 10 SET count TO count + 1
Q4
SET steps TO 0 REPEAT UNTIL steps = 4 SET steps TO steps + 1
Q5
SET water TO 5 REPEAT UNTIL water = 0 SET water TO water - 1
Section E: Spot the Error
Q1
B — The count starts at 1 and goes up, so it can never equal 0. The loop never stops. It should say REPEAT UNTIL count = 5.
Q2
B — Lives start at 3 and go down, so they will never reach 5. The stopping condition must be something the loop works towards: REPEAT UNTIL lives = 0.
Q3
A — Starting at 1 and adding 2 gives 1, 3, 5, 7, 9, 11. It skips 10 and never stops. The start should be SET steps TO 0 so the counting lands on 10.
Q4
C — The score goes down (0, -5, -10...), so it can never reach 20. The loop must add to move towards the condition: SET score TO score + 5.
Q5
D — The variable water never changes, so it never reaches 0 and the loop runs forever. The loop needs SET water TO water - 1.