Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

The While Loop Else Clause

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. You don’t know what that means? Check out this lesson to find out! Furthermore, you can find two examples below, which you can copy-paste and run to get a sense of what’s happening.

n = 5
while n > 0:
    n = n - 1
    if n == 2:
        break
    print(n)
else:
    print("Loop is finished")
n = 5
while n > 0:
    n = n - 1
    if n == 2:
        continue
    print(n)
else:
    print("Loop is finished")

Comments & Discussion

Become a Member to join the conversation.