An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false.

This rule matches the behavior you described:

  • In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
  • When you execute a break statement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause.
  • When you execute a continue statement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause.
  • Other methods of exiting the loop, such as return, do not evaluate the condition and therefore do not run the else clause.

for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.

Answer from drawoc on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_if_else_shorthand.asp
Python Shorthandf If Else
Python If...Else Tutorial If statement If Indentation Elif Else Shorthand If If AND If OR If NOT Nested If The pass keyword in If
🌐
W3Schools
w3schools.com › python › python_if_else.asp
Python Else Statement
Python Examples Python Compiler ... Python Certificate Python Training ... The else keyword catches anything which isn't caught by the preceding conditions....
🌐
Medium
medium.com › @s16h › the-forgotten-optional-else-in-python-loops-90d9c465c830
The Forgotten Optional `else` in Python Loops | by Shahriar Tajbakhsh | Medium
April 28, 2020 - Both for and while loops in Python also take an optional else suite (like the if statement and the try statement do), which executes if the loop iteration completes normally. In other words, the else suite will be executed if we don’t exit the loop in any way other than its natural way.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-if-else
Python If Else Statements - Conditional Statements - GeeksforGeeks
September 16, 2025 - In Python, If-Else is a fundamental conditional statement used for decision-making in programming.
🌐
ScienceDaily
sciencedaily.com › releases › 2026 › 03 › 260313002645.htm
Monty Python Got It Wrong About Medieval Disease | ScienceDaily
1 week ago - Frontiers. "Monty Python Got It Wrong About Medieval Disease." ScienceDaily. www.sciencedaily.com ... Dec. 20, 2025 — Long before opioids flooded communities, something else was quietly changing—and it may have helped set the stage for today’s crisis.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › python-if-else-statement-example
Python If-Else Statement Example
April 10, 2022 - if x < y: print("x is less than y") else: print("x is equal to or greater than y") Note that the fact that Python is a whitespace-sensitive ensures that these if-else statements are easy to read – even when they get nested several layers deep.
🌐
W3Schools
w3schools.com › python › ref_keyword_else.asp
Python else Keyword
x = 5 try: x > 10 except: print("Something went wrong") else: print("The 'Try' code was executed without raising any errors!") Try it Yourself » · The if keyword. The elif keyword. Read more about conditional statements in our Python If...Else Tutorial.
🌐
W3Schools
w3schools.com › python › python_conditions.asp
Python If Statement
Python can evaluate many types of values as True or False in an if statement. Zero (0), empty strings (""), None, and empty collections are treated as False. Everything else is treated as True.
Top answer
1 of 15
220

An if statement runs its else clause if its condition evaluates to false. Identically, a while loop runs the else clause if its condition evaluates to false.

This rule matches the behavior you described:

  • In normal execution, the while loop repeatedly runs until the condition evaluates to false, and therefore naturally exiting the loop runs the else clause.
  • When you execute a break statement, you exit out of the loop without evaluating the condition, so the condition cannot evaluate to false and you never run the else clause.
  • When you execute a continue statement, you evaluate the condition again, and do exactly what you normally would at the beginning of a loop iteration. So, if the condition is true, you keep looping, but if it is false you run the else clause.
  • Other methods of exiting the loop, such as return, do not evaluate the condition and therefore do not run the else clause.

for loops behave the same way. Just consider the condition as true if the iterator has more elements, or false otherwise.

2 of 15
36

Better to think of it this way: The else block will always be executed if everything goes right in the preceding for block such that it reaches exhaustion.

Right in this context will mean no exception, no break, no return. Any statement that hijacks control from for will cause the else block to be bypassed.


A common use case is found when searching for an item in an iterable, for which the search is either called off when the item is found or a "not found" flag is raised/printed via the following else block:

Copyfor items in basket:
    if isinstance(item, Egg):
        break
else:
    print("No eggs in basket")  

A continue does not hijack control from for, so control will proceed to the else after the for is exhausted.

🌐
Exercism
exercism.org › tracks › python › exercises › making-the-grade
Making the Grade in Python on Exercism
>>> for number in range(3, 15, 2): ... if number % 2 == 0: ... print(f"{number} is even.") ... else: ... print(f"{number} is odd.") ... '3 is odd.' '5 is odd.' '7 is odd.' '9 is odd.' '11 is odd.' '13 is odd.' If both values and their indexes are needed, the built-in enumerate(<iterable>) will return (index, value) pairs: >>> word_list = ["bird", "chicken", "barrel", "apple"] # *index* and *word* are the loop variables. # Loop variables can be any valid python name.
🌐
Real Python
realpython.com › ref › keywords › else
else | Python Keywords – Real Python
In Python, the else keyword specifies a block of code to be executed when a certain condition is false in control flow statements such as if, while, and for. It allows you to handle alternative scenarios in your code execution.
🌐
Reddit
reddit.com › r/learnprogramming › python "else" without if
r/learnprogramming on Reddit: python "else" without if
February 10, 2023 -

Hi, I've found this code

num = 407

if num == 1:
    print(num, "is not a prime number")
elif num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")

The "else" outside the for loop doesn't match any "if" before it, but the code still works. How?

🌐
Programiz
programiz.com › python-programming › if-elif-else
Python if, if...else Statement (With Examples)
number = 5 # outer if statement if number >= 0: # inner if statement if number == 0: print('Number is 0') # inner else statement else: print('Number is positive') # outer else statement else: print('Number is negative') ... Here's how this program works. ... In certain situations, the if statement can be simplified into a single line. For example, ... This one-liner approach retains the same functionality but in a more concise format. ... Python doesn't have a ternary operator.
🌐
Python Tips
book.pythontips.com › en › latest › for_-_else.html
21. for/else — Python Tips 0.1 documentation
That is the very basic structure of a for loop. Now let’s move on to some of the lesser known features of for loops in Python. for loops also have an else clause which most of us are unfamiliar with. The else clause executes after the loop completes normally.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.3 documentation
When used with a loop, the else clause has more in common with the else clause of a try statement than it does with that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs.
🌐
DigitalOcean
digitalocean.com › community › tutorials › if-else-statements-in-python
How to Use If/Else Statements in Python: A Beginner’s Guide | DigitalOcean
March 7, 2025 - Conditional statements are a fundamental part of programming, allowing code to make decisions based on certain conditions. In Python, the if/else statement helps control the execution flow by running different blocks of code depending on whether a condition is met or not.
🌐
YouTube
youtube.com › watch
If Else Statements in Python | Python for Beginners
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Python documentation
docs.python.org › 3 › reference › compound_stmts.html
8. Compound statements — Python 3.14.3 documentation
This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.