4

Any code after while loop will execute when the condition in the while loop becomes False. It is the same for the code in the 'else clause' section of while loop in python. So What's the advantage of having 'else' in the while loop?

Kamran Bigdely
  • 7,946
  • 18
  • 66
  • 86

4 Answers4

7

else will not execute if there is a break statement in the loop. From the docs:

The while statement is used for repeated execution as long as an expression is true:

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

(emphasis mine) This also works for forloops, by the way. It's not often useful, but usually very elegant when it is.


I believe the standard use case is when you are searching through a container to find a value:

for element in container:
    if cond(element):
        break
else:
    # no such element

Notice also that after the loop, element will be defined in the global scope, which is convenient.


I found it counterintuitive until I heard a good explanation from some mailing list:

else suites always execute when a condition has been evaluated to False

So if the condition of a while loop is executed and found false, the loop will stop and the else suite will run. break is different because it exits the loop without testing the condition.

Katriel
  • 120,462
  • 19
  • 136
  • 170
1

The else clauses for the looping constructs was to eliminate flags to distinguish between normal and "abnormal" loop exits. For example, in C you might have:

int found = 0;
for(int i = 0; i < BUFSIZ; i++) {
    if(...predicate..) {
       found++;
       break;
    }
}
if(found) {
    // I broke out of the for
} else {
    // the for loop hit BUFSIZ
}

Whereas with a loop-else you can eliminate the (somewhat contrived) found flag

msw
  • 42,753
  • 9
  • 87
  • 112
0

quoting ars: "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."

See Else clause on Python while statement .

Community
  • 1
  • 1
houbysoft
  • 32,532
  • 24
  • 103
  • 156
0

The else suite on Python loops is best thought of for the case where the loop is performing a search. It's where you handle the case where your search was unsuccessful. (There may be other cases where you might use this, but this is the most common and easily remembered user/case).

The alternative would be to use a sentinel value:

sentinel = object()
result = sentinel
for each_item in some_container:
    if matches_some_criteria(each_item):
        result = each_item
        break
if result is sentinel:
    do_something_about_failure()
Jim Dennis
  • 17,054
  • 13
  • 68
  • 116