for-else

Terry Reedy tjreedy at udel.edu
Tue Mar 4 16:40:53 EST 2008


<bearophileHUGS at lycos.com> wrote in message 
news:c4921e6c-119f-4306-8e89-c436c3cb6f6d at u69g2000hse.googlegroups.com...
| So far in Python I've almost hated the 'else' of the 'for' loops:
| - I have problems to remember its meaning;

Consider the following pseudoPython which you should understand:

label: loop
if cond:
   do_something()
   goto loop
else:
  do_else()

We actually write the above as

while cond:
  do_something()
else:
  do_else()

Same meaning; do_else is executed when condition is false.

A for-loop is equivalent to a while loop with the condition 'iterator is 
not exhausted'.  So do_else when that condition is false -- the iterator is 
exhausted.

Terry Jan Reedy






More information about the Python-list mailing list