Loop until condition is true

Ron Adam rrr at ronadam.com
Sat Jun 18 22:22:50 EDT 2005


Joseph Garvin wrote:
> Peter Otten wrote:
> 
>> I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of 
>> "for
>> (" in the Python 2.4 source, so using these rough estimates do-while 
>> still
>> qualifies as "rarely used".
>>
>> Peter
>>
>>  
>>
> That's 136 times you'd have to use an ugly hack instead. I definitely 
> wouldn't mind an until or do/while.

I would happy with just having while: without a 1 or True to indicate a 
continuous loop.  Having a if-break at the end doesn't bother me at all.

    while:
       <suite>
       if <condition>: break

Being able to move the break point to where it's needed or have more 
than one is a feature and not a problem. IMHO of course.

It also has the benefit that you have the option to do an extra bit of 
cleanup between the if and the break if you need to.  The until or 
do-while doesn't give you that option.

I suppose if an until <condition>: <suite>, could be made to be more 
efficient and faster than an if <condition>: <suite>; break,  then I'd 
be for that.

   while:
      <suite>
      until <condition>[: suite]          # optional suite or block
      <suite>

But I doubt it would be significantly faster than an if statement with a 
break. So the only benefit I see is you don't have to use the break 
keyword, and the exit conditions will stand out in blocks with a lot of 
if statements in them.

Regards, Ron









More information about the Python-list mailing list