do ... while loop

Joel Davis joeld at anisci.com
Thu Oct 14 16:33:22 EDT 1999


I totally agree.

I use while 1:  (and for(;;) in c) all the time. It seems like a really
good construct, however I always feel a little uneasy, like it is
abusing the language or something.. 

I wish there was some kind of "officially sanctioned" version of this,
that implied (or even enforced) that the loop was meant to be exited
with a break, somthing like 

loop:
   ...
   if (): 
      break
   ...
   if (): 
      break

This would have three big advantages:

1) It would be clearer as to what the programmer intends. while 1:
implies looping forever, something like loop: would imply that the loop
is intended to be exited by a break

2) the compiler could enforce (or warn) against loops without a break.

3) as you mention, this would get rid of the useless conditional test.

joel

Hrvoje Niksic wrote:
> 
> "Tim Peters" <tim_one at email.msn.com> writes:
> 
> >     while 1:
> >         xxx
> >         yyy
> >         if not condition:
> >             break
> 
> Actually, Python is full of `while 1' loops.  One gets used to them
> after a while (and my C loops with multiple exit points tend to have
> them too), but they're not really nice.  One wonders what's the point
> of having a condition in the while loop if you're not to use it.  :-)

-- 
----------------------------------------------------------------
                                              joeld at anisci.com




More information about the Python-list mailing list