A while loop is used when you want to repeat a block of code as long as a condition remains True.
Unlike a for loop, where we often know what we're iterating over, a while loop is useful when the number of repetitions depends on a condition.
What is a while Loop?
A while loop repeatedly executes a block of code while its condition is True.
The basic syntax is:
while condition:
# code to executeFor example:
count = 1
while count <= 5:
print(count)
count += 1Output:
1
2
3
4
5Python keeps checking count <= 5. Once it becomes False, the loop stops.
How Does a while Loop Work?
You can think of it as:
Check condition
↓
True?
↙ ↘
Yes No
↓ ↓
Run code Stop
↓
Check againFor example:
count = 1
while count <= 3:
print(count)
count += 1The values change like this:
count = 1 → print → increase
count = 2 → print → increase
count = 3 → print → increase
count = 4 → condition is False → stopUpdating the Loop Variable
When using a while loop, make sure something eventually makes the condition false.
count = 1
while count <= 5:
print(count)
count += 1Here, count += 1 increases the value on every iteration.
If you forget it:
count = 1
while count <= 5:
print(count)count will always remain 1, so the condition will always be true.
This creates an infinite loop.
Using while with User Input
A while loop is useful when you want to keep asking the user for something until they provide a particular value.
password = ""
while password != "python123":
password = input("Enter password: ")
print("Access granted!")The program keeps asking until the correct password is entered.
Using break
The break statement immediately stops a loop.
count = 1
while count <= 10:
if count == 5:
break
print(count)
count += 1Output:
1
2
3
4When count becomes 5, break stops the loop.
Using continue
The continue statement skips the current iteration and moves to the next one.
count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)Output:
1
2
4
5The value 3 is skipped.
while with else
Python also allows an else block with a while loop.
count = 1
while count <= 3:
print(count)
count += 1
else:
print("Loop finished")Output:
1
2
3
Loop finishedThe else block runs when the loop finishes normally. If the loop is terminated using break, the else block is skipped.
for vs while
A simple way to decide which loop to use:
|
|
|---|---|
Iterates over a sequence or iterable | Runs while a condition is true |
Often used when processing collections | Useful when the stopping point depends on a condition |
Common with | Common with user input and state-based logic |
For example, use for:
for number in range(5):
print(number)Use while:
number = 0
while number < 5:
print(number)
number += 1Both can produce similar results, but they are useful in different situations.
A Practical Example
Let's create a simple countdown:
count = 5
while count > 0:
print(count)
count -= 1
print("Go!")Output:
5
4
3
2
1
Go!The key idea is simple: a while loop keeps running as long as its condition is true. Just remember to make sure the condition can eventually become false, unless an intentional infinite loop is what you need.