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

Andrew Clover and-google at doxdesk.com
Mon Jul 15 05:47:58 EDT 2002


Rich Harkins <rich at worldsinfinite.com> wrote:

>> while cond1:
>>   dofoo()
>> andwhile cond2:
>>   dobar()

> So does this eqate to the following in current python?

Yes, the latter:

> while cond1:
>   dofoo()
>   if cond2:
>     dobar()
>   else:
>     break

Except that any else-clause following the loop would be unaffected.

>> if cond1:
>>   dofoo()
>> andif cond2:
>>   dobar()
>> else:
>>   doqux()

> and this?

Again, the latter:

> if cond1:
>   dofoo()
>   if cond2:
>     dobar()
>   else:
>     doqux()
> else:
>   doqux()

I find myself writing structures like this very often, in many languages.
I find both current solutions - repeating the else-clause as above, or
using a temporary variable - quite unsatisfactory.

> What if I get more complicated:

> [if ... andif ... elif ... andif]

Two possibilities:

  a. andif has higher precedence than elif, binds to last else/elif
     clause only
  b. this is too confusing to be usable, allow andif only after an
     if or andif, not else/elif.

I prefer (b).

-- 
Andrew Clover
mailto:and at doxdesk.com
http://and.doxdesk.com/



More information about the Python-list mailing list