else with Loops

In Python, you can use else with loops. The else block executes when the loop completes normally (not terminated by break).

else with for Loop

for i in range(5):
    print(i)
else:
    print("Loop completed successfully")

else with while Loop

count = 0
while count < 5:
    print(count)
    count += 1
else:
    print("While loop completed")

else with break

If the loop is terminated by break, the else block is not executed.

for i in range(10):
    if i == 5:
        print("Breaking at", i)
        break
    print(i)
else:
    print("This won't be printed because of break")

Use Cases

The else with loops is useful for:

  • Checking if a loop completed without finding something

  • Performing actions after successful iteration

  • Handling cases where no break occurred

# Example: Finding an item
numbers = [1, 3, 5, 7, 9]
target = 4
for num in numbers:
    if num == target:
        print("Found", target)
        break
else:
    print(target, "not found in the list")