for-else

egbert egbert.bouwman at hccnet.nl
Sat Mar 8 10:20:34 EST 2008


The idea of the if-else is:
.  depending on some condition either do this or do something else, 
.  don't do them both.

If you approach a loop-else with this same idea, you get:
.  depending on the loop conditions either do the loop, 
.  or do something else, but not both.

However the loop-else really works more like this:
.  try to do the loop; 
.  if it starts but is interrupted by a break,
.  then do something else as well.

So they are completely different beasts, and if you try to use
or explain the one according to the rules of the other one,
you put a serious strain on your synapses.

The explanation that the if-else and the loop-else
follow the same pattern, runs more or less like this:
.  all conditions to run the loop to its completion were met,
.  which means that the loop-condition is not met (any more),
.  which means that we must do something else.
For me that is orwellian logic: success is failure.

The loop-else mechanism is not an easy one:
earlier in this thread Carl Banks turned it into an either/or construct
by omitting the crucial break; Jeffrey Barish initially found 
the loop-else surprising; Sion Arrowsmith found it comprehensible 
only after some hard thinking (as in the last paragraph).

The culprit is of course the keyword, else.
The idea of an easy follow-up after a loop is a good one.
But we need other keywords, I think at least three of them:
.  skipped - the loop did not run at all
.  broken - the loop was interrupted (by a break) 
.  exhausted - the loop ran to completion.
The last two as suggested by Laurence Tratt in his Convergence.
e
-- 
Egbert Bouwman - Keizersgracht 197 II - 1016 DS  Amsterdam - 020 6257991
========================================================================



More information about the Python-list mailing list