No, loop-and-a-half! (Re: REPEAT... UNTIL ?)

Greg Ewing see_reply_address at something.invalid
Thu Jul 4 21:47:49 EDT 2002


Ingo Linkweiler wrote:

>
> I know that I can use a while loop with break instead of repeat-until. I 
> am using it, but I just think a repeat-until looks better.


The trouble with repeat-until is that the entire loop always
executes at least once, even if the stopping condition is
true at the outset. The more experience you get at programming,
the more you come to realise that situations where this is
the right behaviour are extremely rare, and in the vast
majority of cases, writing a loop that way is asking for a
bug.

On the other hand, a situation that *is* very common is
a loop-and-a-half, with the exit condition in the middle.
So far, I've never seen *any* really good loop-and-a-half
structure in any language, and I think Python has a chance
to be truly innovative here.

The syntax I favour at the moment for Python looks like

   while:
     line = f.readline()
   gives line <> "":
     do_something()

Some day I'll get around to PEPping this...

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list