do...while

Bjorn Pettersen BPettersen at NAREX.com
Thu Jun 20 18:57:21 EDT 2002


> From: Michael P. Soulier [mailto:msoulier at nortelnetworks.com] 
> 
> On Thu, Jun 20, 2002 at 10:42:56AM -0700, Sean 'Shaleh' Perry wrote:
> > 
> > On 20-Jun-2002 Michael P. Soulier wrote:
> > >     Greetings.
> > > 
> > >     How do you people handle the lack of a do...while in 
> Python? I 
> > > find that at times, I must initialize variables for a loop with 
> > > identical code to the loop, which feels like a waste to me.
> > > 
> > > ie.
> > > 
> > >     line = filehandle.readline()
> > >     while len(line) > 5:
> > >         line = filehandle.readline()
> > > 
> > >     A do...while here would be nice.
> > > 
> > >     Just curious.
> > > 
> > 
> > while 1:
> >     line = filehandle.readline()
> >     if len(line) <= 5: break
> >     ....
> >     ....
> > 
> > is a typical way to implement it.
> 
>     Right, I've used that in the past. It doesn't seem nearly 
> as clean though, as making the conditions for terminating the 
> loop plain in the loop declaration. Considering the Python 
> community's leaning towards readable code, necessitating a 
> break for code factoring goes against that philosophy. 

Doing a quick search over our C++ libraries (~900KLoc), "do {} while()"
seems to be used in right around 3% of the loops (including while, for;
excluding recursion).

Seems like overkill to add special syntax for a feature used so
rarely...

-- bjorn





More information about the Python-list mailing list