While Loops

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

While Loops

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When does a while loop stop?
A After 10 times
B When the condition becomes False
2. i=0\nwhile i<3:\n print(i)\n i=i+1\nWhat prints?
A 0, 1, 2
B 1, 2, 3
3. What happens with while True: and no break?
A The loop runs forever
B It runs once
4. What does the condition in a while loop check?
A Whether to keep looping
B How many times to loop
5. count=5\nwhile count>0:\n print(count)\n count=count-1\nWhat prints first?
A 0
B 5

Match the Pairs

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

Left columnRight column
1while x < 10 x += 1 AOutput: 8
2i = 0 while i < 5: print(i) BOk: Infinite loop with break
3i = 0 while i < 3: i += 1 CValid: Loop runs 3 times
4while True: if done: break DOutput: 1 2 3
5i = 1 while i <= 3: print(i) i += 1 EError: Infinite loop — never increments i
6x = 10 while x > 8: x -= 1 print(x) FError: Missing colon after while

Definitions

while

Fill in the Blanks

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

1.
i = 0 _____ i < 5: print(i) i = i + 1
2.
i = 0 while i < 5_ print(i) i = i + 1
3.
i = 0 while i _ 5: print(i) i = i + 1
4.
i = 0 while i < 5: print(i) i = i _ 1
5.
count = 3 while count _ 0: print(count) count = count - 1

Spot the Error

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

Question 1
A i = 0
B while i < 5:
C print(i)
D i = i + 1
Question 2
A i = 0
B while i < 5
C print(i)
D i = i + 1
Question 3
A i = 0
B while i < 5:
C print(i)
D # missing i = i + 1
Question 4
A i = 0
B While i < 5:
C print(i)
D i = i + 1
Question 5
A i = 0
B while i < 5:
C print(i)
D i = i + 1
E print('Done')

Answer Key

While Loops
Section A: Quick Questions
Q1
B — When the condition becomes False
Q2
A — 0, 1, 2
Q3
A — The loop runs forever
Q4
A — Whether to keep looping
Q5
B — 5
Section B: Match the Pairs
Q1
while x < 10 x += 1 → Error: Missing colon after while
Q2
i = 0 while i < 5: print(i) → Error: Infinite loop — never increments i
Q3
i = 0 while i < 3: i += 1 → Valid: Loop runs 3 times
Q4
while True: if done: break → Ok: Infinite loop with break
Q5
i = 1 while i <= 3: print(i) i += 1 → Output: 1 2 3
Q6
x = 10 while x > 8: x -= 1 print(x) → Output: 8
Section C: Definitions
Q1
while — Repeats indented code as long as the condition is True.~~Condition
Section D: Fill in the Blanks
Q1
i = 0 while i < 5: print(i) i = i + 1
Q2
i = 0 while i < 5: print(i) i = i + 1
Q3
i = 0 while i < 5: print(i) i = i + 1
Q4
i = 0 while i < 5: print(i) i = i + 1
Q5
count = 3 while count > 0: print(count) count = count - 1
Section E: Spot the Error
Q1
C — Code inside the while loop must be indented
Q2
B — Missing colon at the end of the while line
Q3
C — Missing i = i + 1: the counter never increases, causing an infinite loop
Q4
B — while must be lowercase: While is not valid Python
Q5
E — 'Done' is indented inside the loop it prints 5 times instead of once