While Loops
The while loop executes code repeatedly as long as a condition is true.
Unlike for loops that iterate a known number of times, while loops are perfect when
you don't know in advance how many iterations you need!
Basic While Loop
A while loop checks its condition before each iteration. When the condition becomes False, the loop stops and execution continues after the loop.
while condition:
# code to execute (repeat while condition is True)
# IMPORTANT: Update something to eventually make condition False!
Click Run to execute your code
for loops when you know how many times
to iterate (or are iterating over a sequence). Use while loops when the
number of iterations depends on a condition that changes during execution.
The while...else Clause
Python's while loop has an optional else clause that runs when the condition
becomes False normally. If the loop exits via break, the else is skipped.
Click Run to execute your code
while...else pattern is useful for search loops.
The else block runs only if the item wasn't found (no break). Think of it as
"if not found" code!
Practical Examples
While loops excel at input validation, processing queues, retry logic, and any situation where you repeat until a condition is met.
Click Run to execute your code
Infinite Loops: Danger and Use Cases
An infinite loop occurs when the condition never becomes False. Usually this is a bug,
but sometimes while True: with a break is a valid pattern.
Click Run to execute your code
break statement that will eventually execute. Consider adding a failsafe
maximum iteration count!
Common Mistakes
1. Forgetting to update the loop variable
# Wrong - infinite loop!
count = 1
while count <= 5:
print(count)
# Missing: count += 1
# Correct
count = 1
while count <= 5:
print(count)
count += 1
2. Off-by-one errors
# Wrong - prints 1-4, not 1-5
count = 1
while count < 5: # Should be <= 5
print(count)
count += 1
# Correct - prints 1-5
count = 1
while count <= 5:
print(count)
count += 1
3. Updating in wrong direction
# Wrong - counting up when should count down!
n = 10
while n > 0:
print(n)
n += 1 # Should be n -= 1!
# Correct
n = 10
while n > 0:
print(n)
n -= 1
4. Condition never matches
# Wrong - 5 can never equal 10 with step of 2
x = 5
while x != 10:
print(x)
x += 2 # x: 5, 7, 9, 11, 13... never 10!
# Correct - use >= or <=
x = 5
while x < 10:
print(x)
x += 2
Exercise: Countdown Timer
Task: Create a countdown using a while loop.
Requirements:
- Print numbers from 10 down to 1
- Print "Blastoff!" after the loop
Click Run to execute your code
Show Solution
start = 10
current = start
while current > 0:
print(current)
current -= 1 # Decrease by 1 each iteration
print("Blastoff!")
Summary
- while condition: Repeats while condition is True
- Must update something to eventually make condition False
- while...else: else runs only if loop completes normally (no break)
- while True: Intentional infinite loop, must have break
- Use cases: Input validation, retry logic, processing until done
- Danger: Infinite loops when condition never becomes False
What's Next?
Now that you know both for and while loops, let's learn about loop control statements
- break, continue, and pass. These give you fine-grained
control over loop execution!
Enjoying these tutorials?