Infinite loops and how to stop them

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

Infinite loops and how to stop them

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which loop is an infinite loop?
A count = 1 while count <= 5: print(count)
B count = 1 while count <= 5: print(count) count = count + 1
2. Why does this loop never stop? while True: print('Hi')
A The print() function is written wrongly
B The condition is always True and can never be False
3. Your program is stuck in an infinite loop and keeps printing. What should you do first?
A Click the Stop button
B Wait for it to finish by itself
4. What is the best fix for a loop that forgot to update its variable?
A Add count = 1 before the loop
B Add count = count + 1 inside the loop
5. Which line placed inside a while True: loop gives it a way to exit?
A if count == 5: break
B if count == 5: print(count)

Match the Pairs

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

Left columnRight column
1while True: print("Hi") AUpdates the loop variable each time round so the while condition can eventually become False
2count = count + 1 Bwhile count < 3: count = count + 1
3Adding break inside the loop is one way to exit a loop that would otherwise run forever Cwhile True: if count == 5: break
4A loop with an updated variable ends because its condition finally becomes False DA condition that can never be False, so this loop runs forever unless you use break or press Stop
5count = 0 while count < 5: print(count) Ecount never changes inside the loop, so count < 5 stays True forever. Add count = count + 1 inside the loop.
6count = 10 while count > 0: count = count + 1 Fcount moves away from 0, so count > 0 can never become False. Change it to count = count - 1.

Definitions

infinite loop

Fill in the Blanks

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

1.
count = 0 while count < 3: _____ = count + 1
2.
while True: if input("Quit? ") == "y": _____
3.
while ____: print("Hello")
4.
count = 10 while count > 0: count = count _ 1
5.
count = 0 _____ count < 3: count = count + 1

Spot the Error

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

Question 1
A count = 1
B while count <= 5:
C count = count - 1
D print(count)
Question 2
A while True
B word = input("Word: ")
C if word == "quit":
D break
Question 3
A count = 1
B while count > 0:
C print(count)
D count = count + 1
Question 4
A count = 0
B while count < 3:
C counter = count + 1
D print("Hi")
Question 5
A while True:
B answer = input("Quit? ")
C if answer == "yes":
D continue

Answer Key

Infinite loops and how to stop them
Section A: Quick Questions
Q1
A — count = 1 while count <= 5: print(count)
Q2
B — The condition is always True and can never be False
Q3
A — Click the Stop button
Q4
B — Add count = count + 1 inside the loop
Q5
A — if count == 5: break
Section B: Match the Pairs
Q1
while True: print("Hi") → A condition that can never be False, so this loop runs forever unless you use break or press Stop
Q2
count = count + 1 → Updates the loop variable each time round so the while condition can eventually become False
Q3
Adding break inside the loop is one way to exit a loop that would otherwise run forever → while True: if count == 5: break
Q4
A loop with an updated variable ends because its condition finally becomes False → while count < 3: count = count + 1
Q5
count = 0 while count < 5: print(count) → count never changes inside the loop, so count < 5 stays True forever. Add count = count + 1 inside the loop.
Q6
count = 10 while count > 0: count = count + 1 → count moves away from 0, so count > 0 can never become False. Change it to count = count - 1.
Section C: Definitions
Q1
infinite loop — A while loop whose condition never becomes False, often because the loop variable is never updated. Stop it with the Stop button, or fix it by changing the variable inside the loop.
Section D: Fill in the Blanks
Q1
count = 0 while count < 3: count = count + 1
Q2
while True: if input("Quit? ") == "y": break
Q3
while True: print("Hello")
Q4
count = 10 while count > 0: count = count - 1
Q5
count = 0 while count < 3: count = count + 1
Section E: Spot the Error
Q1
C — count goes down, so count <= 5 is always True and the loop never ends. It should be count = count + 1 so count grows towards 5.
Q2
A — A while line must end with a colon. It should be while True: and the break inside is what stops this loop.
Q3
B — count starts at 1 and only goes up, so count > 0 can never be False and the loop runs forever. It should be while count < 5:.
Q4
C — This updates a different variable called counter, so count never changes and the loop never ends. It should be count = count + 1.
Q5
D — continue jumps back to the top of the loop instead of leaving it, so the loop never stops. It should be break to exit the loop.