Chapter 3: The while Loop — Keep Going While True¶
Why Do We Need Loops?¶
When you need to run the same code multiple times, use a loop.
# Without a loop: print 1~5
print(1)
print(2)
print(3)
print(4)
print(5)
What if you needed 100 times? Use a loop.
while Basic Structure¶
while condition:
code to execute
If the condition is True, the block runs, then the condition is checked again. When the condition becomes False, the loop exits.
Check condition
↓ True
Execute code
↓
Check condition ← go back
↓ False
Loop ends
Understanding Through Examples¶
Example 1: Counter¶
i = 0
while i < 5:
i += 1
print(i)
5
i keeps increasing by 1 inside the loop. When i < 5 becomes False (i = 5), the loop ends.
Example 2: Printing Inside the Loop¶
i = 0
while i < 5:
print(i)
i += 1
print(i)
0
1
2
3
4
5
print(i) comes before i += 1, so it starts from 0.
Example 3: Running Total¶
i = 0
total = 0
while i < 5:
print(i * '#') # print i '#' characters
total += i
i += 1
print(total)
(blank line)
#
##
###
####
10
0+1+2+3+4 = 10
break — Immediate Exit¶
Ends the loop immediately without waiting for the condition.
i = 0
while i < 10:
if i == 5:
break
print(i)
i += 1
0
1
2
3
4
When i reaches 5, break exits the entire loop.
continue — Skip This Iteration¶
Skips the rest of the current iteration and jumps back to the condition check.
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue
print(i)
1
3
5
7
9
When i is even, continue skips print(i).
while True — Infinite Loop¶
Using True as the condition makes the loop run forever. Use break to escape.
while True:
cmd = input("Enter command (q=quit): ")
if cmd == "q":
break
print("Input:", cmd)
Enter command (q=quit): hello
Input: hello
Enter command (q=quit): q
Useful when you don't know the exit condition in advance or need continuous user input.
Fibonacci Sequence¶
x, y = 0, 1
while x < 100:
print(x, end=" ")
x, y = y, x + y
0 1 1 2 3 5 8 13 21 34 55 89
x, y = y, x + y simultaneously updates both variables.
| Step | x | y |
|---|---|---|
| Start | 0 | 1 |
| 1st | 1 | 1 |
| 2nd | 1 | 2 |
| 3rd | 2 | 3 |
| 4th | 3 | 5 |
| 5th | 5 | 8 |
Practice Missions¶
Mission 1: Multiplication Table¶
Take a number as input and print its multiplication table.
dan = int(input("Table for: "))
i = 1
# Use a while loop
# Table for: 3
# 3 × 1 = 3
# 3 × 2 = 6
# ...
# 3 × 9 = 27
Mission 2: Number Guessing Game¶
The computer picks a random number between 1 and 100. Give hints until the user guesses it.
import random
secret = random.randint(1, 100)
# Use a while loop
# Hints: "Higher", "Lower", "Correct!"
# Guess the number: 50
# Higher!
# Guess the number: 75
# Lower!
# Guess the number: 63
# Correct! You got it in 3 tries
Mission 3: Prime Number Checker¶
Take a number and determine if it's prime. (Use a while loop)
Primes: 2, 3, 5, 7, 11, 13, ...
If divisible by any number from 2 to n-1, it's not prime
n = int(input("Number: "))
# Write your code here
# Number: 17
# 17 is prime
# Number: 15
# 15 is not prime
Mission 4: Running Total¶
Starting from 1, find the number when the sum first exceeds 100.
total = 0
n = 0
# Use a while loop
# Sum exceeded 100 at: 14 (1+2+...+13=91, 1+2+...+14=105)
Mission 5: Fibonacci Extended¶
Print all Fibonacci numbers up to 1000 and count them.
x, y = 0, 1
count = 0
# Write your code here
# 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# Total: 17 numbers
Key Summary¶
| Concept | Description |
|---|---|
while condition: |
Repeat while condition is True |
break |
Exit loop immediately |
continue |
Skip rest of iteration, return to condition |
while True: |
Infinite loop (escape with break) |
| Counter variable | Increment inside loop to change condition |
x, y = y, x+y |
Simultaneous variable update |