A while loop in Python repeatedly executes a block of code as long as a condition remains True.
The loop stops automatically when the condition becomes False.
while loops are most useful when:
- The number of iterations is unknown
- Execution depends on a changing condition
- You want to keep running until something happens (user input, event, state change)
while condition:
# code runs while condition is True| Concept | Meaning |
|---|---|
| Condition | Expression evaluated as True or False |
| Loop body | Code executed repeatedly |
| Iteration | One cycle of execution |
| Exit condition | Condition that stops the loop |
| Infinite loop | Condition never becomes False |
count = 1
while count <= 5:
print(count)
count += 1Output:
1
2
3
4
5
count += 1 is critical — without it, the loop would run forever.
An infinite loop runs forever unless explicitly stopped.
while True:
print("This loop will run forever")
breakOutput:
This loop will run forever
True never becomes False, so you must use break.
The else block executes only if the loop ends normally (not via break).
x = 0
while x < 3:
print(x)
x += 1
else:
print("Loop has finished")Output:
0
1
2
Loop has finished
x = 0
while x < 3:
if x == 1:
break
print(x)
x += 1
else:
print("Loop has finished")Output:
0
else does not run because the loop exited via break.
break immediately terminates the loop.
x = 0
while x < 10:
if x == 5:
break
print(x)
x += 1Output:
0
1
2
3
4
continue skips the current iteration and jumps back to the condition check.
x = 0
while x < 5:
x += 1
if x == 3:
continue
print(x)Output:
1
2
4
5
3 is skipped.
Common real-world use case: keep asking until correct input.
password = ""
while password != "secret":
password = input("Enter the password: ")
print("Access granted!")Loop continues until the correct password is entered.
A while loop inside another while loop.
i = 1
while i <= 3:
j = 1
while j <= 2:
print(f"i = {i}, j = {j}")
j += 1
i += 1Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
total = 0
while True:
user_input = input("Enter a number (or 'q' to quit): ")
if user_input == 'q':
break
total += int(user_input)
print("Total sum is:", total)This pattern is extremely common in real applications.
-
Waiting for a condition
- User input
- File availability
- Network response
-
Unknown number of iterations
- Reading data until EOF
- Retrying until success
-
Real-time monitoring
- Background services
- Event listeners
- Game loops
| Mistake | Why It Happens |
|---|---|
| Infinite loop | Condition never changes |
| Forgetting increment | Counter not updated |
| Wrong exit condition | Loop never stops |
Overusing while |
for loop might be simpler |
| Nested infinite loops | Program becomes unresponsive |
| Feature | for Loop |
while Loop |
|---|---|---|
| Known iterations | Yes | No |
| Condition-based | No | Yes |
| Cleaner syntax | Yes | Depends |
| Risk of infinite loop | Low | High |
- Print numbers from 10 to 1 using a
whileloop. - Ask the user to enter numbers until they enter
0, then print the sum. - Create a password checker with a maximum of 3 attempts.
- Print all even numbers between 1 and 50 using
while. - Use a nested
whileloop to print a multiplication table (1–5).