While–Else in Python | Complete Beginner Guide with Examples

When learning Python, most beginners are familiar with:

  • if–else
  • while loop

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:

  • else does 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:

  1. Loop ends normally (no break) → else executes
  2. Loop ends using breakelse is 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 break statement is used
  • Loop completes all iterations
  • Condition becomes False naturally
  • else block 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 i becomes 2, break executes
  • Loop stops immediately
  • Since loop did not complete naturally, else is 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

  • continue skips only that iteration
  • Loop still completes normally
  • else executes

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
  • else never 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

  • return exits the function completely
  • Loop does not finish normally
  • else does 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
  • else is skipped

11. Final Rule You Should Remember

The else block executes only when the while loop finishes normally, without break, 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

  • while is the loop
  • else is an optional block
  • else runs only if the loop completes naturally
  • break, return, infinite loops, and errors skip else
  • 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.

Leave a Comment