Else clause on Python while statement Else clause on Python while statement python python

Else clause on Python while statement


The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won't be executed.

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()


The else clause is executed if you exit a block normally, by hitting the loop condition or falling off the bottom of a try block. It is not executed if you break or return out of a block, or raise an exception. It works for not only while and for loops, but also try blocks.

You typically find it in places where normally you would exit a loop early, and running off the end of the loop is an unexpected/unusual occasion. For example, if you're looping through a list looking for a value:

for value in values:    if value == 5:        print "Found it!"        breakelse:    print "Nowhere to be found. :-("


In reply to Is there a specific reason?, here is one interesting application: breaking out of multiple levels of looping.

Here is how it works: the outer loop has a break at the end, so it would only be executed once. However, if the inner loop completes (finds no divisor), then it reaches the else statement and the outer break is never reached. This way, a break in the inner loop will break out of both loops, rather than just one.

for k in [2, 3, 5, 7, 11, 13, 17, 25]:    for m in range(2, 10):        if k == m:            continue        print 'trying %s %% %s' % (k, m)        if k % m == 0:            print 'found a divisor: %d %% %d; breaking out of loop' % (k, m)            break    else:        continue    print 'breaking another level of loop'    breakelse:    print 'no divisor could be found!'

For both while and for loops, the else statement is executed at the end, unless break was used.

In most cases there are better ways to do this (wrapping it into a function or raising an exception), but this works!


matomo