The else clause is only executed after the while condition is evaluated to be false.

Thus, if you break out of the loop, or if an exception is raised, the else won't be executed (since the while condition has not been evaluated to be false yet).

One way to think about it is as an if/else construct with respect to the condition:

if condition:
    handle_true()
else:
    handle_false()

is analogous to the looping construct:

while condition:
    handle_true()
else:
    # condition is false now, handle and go on with the rest of the program
    handle_false()

An example might be along the lines of:

while value < threshold:
    if not process_acceptable_value(value):
        # something went wrong, exit the loop; don't pass go, don't collect 200
        break
    value = update(value)
else:
    # value >= threshold; pass go, collect 200
    handle_threshold_reached()
Answer from ars on Stack Overflow
🌐
W3Schools
w3schools.com › python › gloss_python_while_else.asp
Python While Else
Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro ...
Discussions

While loops can have an else statement!
You're almost right! The code under the else will be executed only when the while loop is not "broke out of". In the following code, if you change the variable guess to the value 10. The else clause will be executed. l = [4,5,6,7,8,9] count = 0 guess = 5 while count < len(l): if l[count] == guess: break count += 1 else: # if guess == 5 this will not be executed print("guess not found") print(count) More on reddit.com
🌐 r/learnpython
4
9
January 31, 2024
TIL you can use else with a while loop
You can also use it with for! More on reddit.com
🌐 r/Python
197
641
March 1, 2025
Examples of the while...else or for...else constructs in the wild?
I use a for/else in my code for a Miller-Rabin primality checker: def isPrime(n, k=5): # miller-rabin if n < 2: return False for p in [2,3,5,7,11,13,17,19,23,29]: if n % p == 0: return n == p s, d = 0, n-1 while d % 2 == 0: s, d = s+1, d/2 for i in range(k): x = pow(randint(2, n-1), d, n) if x == 1 or x == n-1: continue for r in range(1, s): x = (x * x) % n if x == 1: return False if x == n-1: break else: return False return True More on reddit.com
🌐 r/Python
34
13
November 13, 2014
Why do for loops have an else block?
https://book.pythontips.com/en/latest/for_-_else.html More on reddit.com
🌐 r/Python
129
307
August 25, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-else
Python While Else - GeeksforGeeks
July 23, 2025 - In Python, the while loop is used for iteration. It executes a block of code repeatedly until the condition becomes false and when we add an "else" statement just after the while loop it becomes a "While-Else Loop".
🌐
Python Tutorial
pythontutorial.net › home › python basics › python while else
Python while...else Statement
March 31, 2025 - The else clause in the while else statement will execute when the condition of the while loop is False and the loop runs normally without encountering the break or return statement.
🌐
Reddit
reddit.com › r/learnpython › while loops can have an else statement!
r/learnpython on Reddit: While loops can have an else statement!
January 31, 2024 -

Maybe I'm the only one who didn't know this, but you can put an else statement after a while loop which will be used if the while loop is never entered. I found this out on the Leetcode problem of the day for today when I tried putting an else after my while loop, mostly expecting an error but typing because it's what I wanted the code to do, and it ran! The code I used is below. I know there are more optimal solutions, but I'm happy with what I learned today.

class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
    n = len(temperatures)
    answers = [0] * n
    stack = []
    for i in range(n):
        while len(stack) > 0 and temperatures[i] > stack[-1][1]:
            answers[stack[-1][0]] = i - stack[-1][0]
            stack.pop()
        else:
            stack.append([i, temperatures[i]])
        if len(stack) == 0:
            stack.append([i, temperatures[i]])
    return answers

🌐
Codefinity
codefinity.com › courses › v2 › a8aeafab-f546-47e9-adb6-1d97b2927804 › 98dca52d-9dbe-41d0-9260-dd6a4c00f21e › d2099d74-36c9-4479-b70a-e3049bdcdfbe
Learn The else Statement in a while Loop | The while Loop
The else block executes when the loop terminates normally, meaning the loop condition becomes False without encountering a break statement. 1234567891011 travel_list = ['Monako', 'Luxemburg', 'Liverpool', 'Barcelona', 'Munchen'] # Initialize ...
🌐
Medium
allwin-raju.medium.com › understanding-the-while-else-loop-in-python-436c791649e8
Understanding the while-else Loop in Python | by Allwin Raju | Medium
November 12, 2024 - The else block in a while-else loop only executes if the while loop finishes without encountering a break statement. If a break statement terminates the loop early, Python skips the else block entirely.
Find elsewhere
🌐
TOOLSQA
toolsqa.com › python › python-while-loop
Python While Loop | While True and While Else in Python || ToolsQA
August 6, 2021 - Do else statements get skipped? Or does "else" get executed? If there is a while loop in python with a break statement and an else statement, the else statement skips when the"break" executes.
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - Python’s while loops also have additional syntax. They have an else clause that runs when the loop terminates naturally because the condition becomes true. In the following sections, you’ll learn more about how the break and continue statements ...
🌐
Python Examples
pythonexamples.org › python-while-else
Python While Else
Python While Else executes else block when the while condition becomes False. Syntax and working is same as that of Python While, but has an additional else block after while block. ... The flow of execution for a while else statement is illustrated in the following diagram.
🌐
Real Python
realpython.com › lessons › while-loop-else-clause
The While Loop Else Clause (Video) – Real Python
Join us and get access to thousands of tutorials and a community of expert Pythonistas. ... This lesson covers the while-loop-else-clause, which is unique to Python. The else-block is only executed if the while-loop is exhausted.
Published   March 1, 2019
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
counter = 0 while counter < 2: print('This is inside loop') counter = counter + 1 ... Here, on the third iteration, the counter becomes 2 which terminates the loop. It then executes the else block and prints This is inside else block.
🌐
Python documentation
docs.python.org › 3 › tutorial › controlflow.html
4. More Control Flow Tools — Python 3.14.6 documentation
In a for loop, the else clause is executed after the loop finishes its final iteration, that is, if no break occurred. In a while loop, it’s executed after the loop’s condition becomes false.
🌐
Towards Data Science
towardsdatascience.com › home › latest › python for-else and while-else clearly explained with 4 real-world examples
Python For-Else and While-Else Clearly Explained with 4 Real-World Examples | Towards Data Science
March 5, 2025 - For a moment, stop trying to make sense of the wordelse`. Just think that Python is offering you an additional feature with its loops. Let us see how it works. ... The else clause will not be executed if the loop gets terminated by a break statement. If a loop does not hit a break statement, then the else clause will be executed once after the loop has completed all its iterations (meaning, after the loop has completed normally). Image by Author – For-Else and While-Else in Python
🌐
Reddit
reddit.com › r/python › til you can use else with a while loop
r/Python on Reddit: TIL you can use else with a while loop
March 1, 2025 -

Not sure why I’ve never heard about this, but apparently you can use else with a while loop. I’ve always used a separate flag variable

This will execute when the while condition is false but not if you break out of the loop early.

For example:

Using flag

nums = [1, 3, 5, 7, 9]
target = 4
found = False
i = 0

while i < len(nums):
    if nums[i] == target:
        found = True
        print("Found:", target)
        break
    i += 1

if not found:
    print("Not found")

Using else

nums = [1, 3, 5, 7, 9]
target = 4
i = 0

while i < len(nums):
    if nums[i] == target:
        print("Found:", target)
        break
    i += 1
else:
    print("Not found")
🌐
Scaler
scaler.com › home › topics › python › python for else
Python For Else - Scaler Topics
December 1, 2023 - In Python, 'for-else' and 'while-else' constructs provide a unique way to handle loops with an additional clause executed when the loop usually concludes without a break statement.
🌐
Tutorjoes
tutorjoes.in › python_programming_tutorial › while_else_and_for_else_in_python
While Else and For Else in Python
In the given code, we don't have any condition to break the loop that's why the both loops are running completely and printing ... # While Else & For Else i=1 while i<=5: #if(i==4): #break print(i) i+=1 else: print("Loop Completed") for i in range(1,21): #if i==5: #break print(i) else: print("For Loop Completed") To download raw file Click Here
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › python while else
Python While Else: Loop, True Else, Break | StudySmarter
In Python, the while else statement is a combination of a while loop with an else block. The while loop executes a block of code as long as its given condition remains true. Once the condition becomes false, the loop stops, and the else block is executed.
🌐
Perlego
perlego.com › index › computer-science › python-while-else
Python while else | Overview & Research Examples
In Python, the "while else" statement is used to execute a block of code within the "else" clause after the while loop has completed its iterations. If the while loop terminates normally (i.e., without encountering a "break" statement), the ...