When learning Python, most beginners are familiar with:
if–elsewhileloop
But very few people know that Python also supports else with loops.
This feature is unique to Python and often considered one of the most underrated concepts.
In this blog, we’ll start from the basics and slowly move to while–else, explaining how it works, when it executes, and when it doesn’t — with clear examples and outputs.
1. Understanding if–else in Python
The if–else statement executes code based on a condition.
Syntax
if condition:
# code if condition is True
else:
# code if condition is False
Example
age = 18
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible")
Output
Eligible to vote
Here, else runs when the condition becomes False.
2. Understanding the while Loop
A while loop repeats a block of code as long as a condition is True.
Syntax
while condition:
# loop body
Example
i = 1
while i <= 3:
print(i)
i += 1
Output
1 2 3
The loop stops when the condition i <= 3 becomes False.
3. Introducing while–else in Python
In Python, a while loop can have an optional else block.
Syntax
while condition:
# loop code
else:
# executes only if loop ends normally
⚠️ Important:
elsedoes not run after every loop- It runs only once
- It runs only if the loop completes naturally
4. How while–else Actually Works
Python does not check the condition to decide whether to execute else.
Instead, Python checks how the loop ended.
There are only two main cases:
- Loop ends normally (no
break) →elseexecutes - Loop ends using
break→elseis skipped
5. Case 1: Loop Ends Naturally (Else Executes)
Example
i = 1
while i <= 3:
print(i)
i += 1
else:
print("Done")
Output
1 2 3 Done
Explanation
- No
breakstatement is used - Loop completes all iterations
- Condition becomes False naturally
elseblock executes
6. Case 2: Loop Ends Using break (Else Skipped)
Example
i = 1
while i <= 3:
if i == 2:
break
print(i)
i += 1
else:
print("Done")
Output
1
Explanation
- When
ibecomes2,breakexecutes - Loop stops immediately
- Since loop did not complete naturally,
elseis skipped
7. What About continue?
continue does NOT stop the loop, it only skips the current iteration.
Example
i = 0
while i < 3:
i += 1
if i == 2:
continue
print(i)
else:
print("Done")
Output
1 3 Done
Explanation
continueskips only that iteration- Loop still completes normally
elseexecutes
8. Infinite Loop Case (Else Never Executes)
Example
while True:
print("Hello")
else:
print("Done")
Output
Hello Hello Hello ...
Explanation
- Loop never ends
- Condition never becomes False
elsenever executes
9. Using return Inside a While Loop
⚠️ return can be used only inside a function.
Example
def demo():
i = 1
while i < 5:
if i == 3:
return
print(i)
i += 1
else:
print("Done")
demo()
Output
1 2
Explanation
returnexits the function completely- Loop does not finish normally
elsedoes not execute
10. What About Errors / Exceptions?
If an exception occurs inside the loop, else will not execute.
Example
while True:
x = 1 / 0
else:
print("Done")
Explanation
- Program crashes
- Loop does not complete normally
elseis skipped
11. Final Rule You Should Remember
The
elseblock executes only when thewhileloop finishes normally, withoutbreak,return, or errors.
12. Common Beginner Mistakes
❌ Thinking else belongs to if
❌ Thinking else runs when condition is False only
❌ Forgetting indentation
❌ Using break and expecting else to run
13. Is while–else Available in Other Languages?
No.
while–else exists only in Python.
Languages like:
- C
- C++
- Java
- JavaScript
do not support loop-else.
14. Summary
whileis the loopelseis an optional blockelseruns only if the loop completes naturallybreak,return, infinite loops, and errors skipelse- This feature is unique to Python
If you found this explanation helpful, explore more Python concepts at udaycodes.in
For doubts or topic requests, feel free to reach out through the website.