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?
4 Answers
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 theelse
clause’s suite. Acontinue
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 for
loops, 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 toFalse
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.

- 120,462
- 19
- 136
- 170
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

- 42,753
- 9
- 87
- 112
-
The `found` variable is unnecessary even in the C code, you could use `if(i!=BUFSIZ)` instead of `if(found)`. – houbysoft Dec 26 '10 at 04:58
-
Agreed, thus the "contrived" bit. – msw Dec 26 '10 at 04:59
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."
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()

- 17,054
- 13
- 68
- 116