devnotes.

Loop Control: break, continue, and the else Clause

break and continue work as in most languages, but Python has a surprising extra: loops can have an else clause, which most other languages don't offer.

for n in range(2, 10):
    if n % 2 == 0:
        continue          # skip the rest of this iteration, go to the next n
    print(n)               # only prints odd numbers

for n in range(10):
    if n == 100:
        break               # never reached here
    print(n)
else:
    print("Loop finished without breaking")   # this DOES run — the loop wasn't broken

target = 7
numbers = [1, 3, 5, 9]
for n in numbers:
    if n == target:
        print("Found it!")
        break
else:
    print("Not found")   # runs only if the loop completed without a break

continue skips the rest of the current iteration's body and jumps straight to the next one; break exits the loop entirely, skipping any remaining iterations.

A loop's else clause runs only if the loop completes without hitting a break — it's easy to misread at first (it doesn't mean "otherwise" the way an if/else does), but it's genuinely useful for exactly one common pattern: searching for something and needing to know whether it was found. The for...else block above is the idiomatic Python way to express "if not found in the loop, do this" — cleaner than a separate found = False flag variable set inside the loop and checked afterward.

Loop Control in Python: break, continue, and for-else | DevNotes