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
🌐
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")
Discussions

Benefits of else clauses on while loops - Discussion - Swift Forums
A nifty language feature I've built into languages I've created in the past is "welse" (while...else). The syntax is basically: while expr { *while body* } welse { *code to execute when expr is false* } Note that if the while loop exits for any reason other than the expression being false (such ... More on forums.swift.org
🌐 forums.swift.org
2
March 2, 2025
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
While loop with else statement - C++ Forum
How do I stop the infinite loop, and why did it start in the first place? I'm hoping it's a simple mistake since the else in the main function displays Invalid correctly, and although both elses are the same, (at least look the same, this is probably where I went wrong) it initiates the loop. More on cplusplus.com
🌐 cplusplus.com
WHich loop to use and when? do/while, for, else, if else, while, goto
Trying to figure out which loop to use. I've read the Arduino reference, so in theory i get it. But what I'm after are simple examples. Can someone point me to where I can find some simple examples comparing the different loops and why and when to use one loop command instead of another. Thanks More on forum.arduino.cc
🌐 forum.arduino.cc
8
0
May 25, 2018
🌐
W3Schools
w3schools.com › python › gloss_python_while_else.asp
Python While Else
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else
🌐
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".
🌐
Swift Forums
forums.swift.org › evolution › discussion
Benefits of else clauses on while loops - Discussion - Swift Forums
March 2, 2025 - The syntax is basically: while expr { *while body* } welse { *code to execute when expr is false* } Note that if the while loop exits for any reason other than the expression being false (such ...
🌐
Experts PHP
expertsphp.com › while-loop-with-else-statement-use-in-python
While Loop With else Statement Use in Python - Experts PHP
September 20, 2019 - The relation of while Loop else is made. As long as the condition is true, while the statement of the while loop iterates and the condition is false then the control pass is passed and executing the else statement.
Find elsewhere
🌐
Codecademy
codecademy.com › forum_questions › 509fa70f44ee00bae7000ca7
Did you include an 'else' for the while loop? | Codecademy
while count < 4: if count == 1: ... elif count == 4: print "You lose." else: print "Try agian" ... The else loop is supposed to go after the while loop....
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-while-loop
while Loop in C - GeeksforGeeks
The while loop in C allows a block of code to be executed repeatedly as long as a given condition remains true.
Published   October 8, 2025
🌐
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

🌐
Cplusplus
cplusplus.com › forum › beginner › 206676
While loop with else statement - C++ Forum
The second step is to realize that you need to better constrain the else if (much >= 8) conditional. Otherwise 11, which is outside the range, is not invalid. What really happens when you enter a value between 2^31 (2147483648) and 2^32-1 (4294967295)? The value in much is actually negative.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
WHich loop to use and when? do/while, for, else, if else, while, goto - Programming - Arduino Forum
May 25, 2018 - Trying to figure out which loop to use. I've read the Arduino reference, so in theory i get it. But what I'm after are simple examples. Can someone point me to where I can find some simple examples comparing the differe…
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-else-loop
Python Else Loop - GeeksforGeeks
July 28, 2020 - Else with loop is used with both while and for loop. The else block is executed at the end of loop means when the given loop condition is false then the else block is executed.
🌐
W3Schools
w3schools.com › python › gloss_python_for_else.asp
Python For Else
Note: The else block will NOT be executed if the loop is stopped by a break statement.
🌐
Medium
medium.com › @andrewdass › python-for-loops-while-loops-and-if-else-statements-2ecc134953ca
Python: For Loops, While Loops and If-Else Statements | by Andrew Dass | Medium
February 12, 2024 - index = 0 while index < 3: print(index) ... executed: ... An if-else statement goes through two conditions to see if the specified condition from the if statement is true, otherwise, the else statement will be printed....
🌐
Du
cs.du.edu › ~intropython › byte-of-python › control_flow.html
if Statements and while/for Loops · HonKit
You can use an if..elif..else statement to do the same thing (and in some cases, use a dictionary to do it quickly) The while statement allows you to repeatedly execute a block of statements as long as a condition is true. A while statement is an example of what is called a looping statement.
🌐
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.
🌐
Javaexercise
javaexercise.com › python › python-while-and-while-else-loop
Python While Loop | While-else | Nested While | Infinite While Loop - Javaexercise
July 27, 2022 - # Single statement while loop in python i = 0 while i<3: print(i) ... In this topic, we have learned the use and advantages of while and while-else loops in a Python program, following a running example of a simple iterative printing program, thus giving us an intuition of how this concept could be applied in real-world situations.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › difference-between-for-while-and-do-while-loop-in-programming
Difference between For, While and Do-While Loop in Programming - GeeksforGeeks
July 23, 2025 - For Loop, While Loop, and Do-While Loop are different loops in programming. A For loop is used when the number of iterations is known. A While loop runs as long as a condition is true.