PEP 315: Enhanced While Loop

Andrew Koenig ark at research.att.com
Mon May 5 08:52:17 EDT 2003


> Python is not broken. But if Python could not be improved we wouldn't
> need PEPs at all.

Agreed.

> * "do" is unclear
> * do-while in other languages doesn't have 2nd suite
> * do-while-while ambiguity
> * unseen "do" changes the meaning of seen "while"

On the other hand, I proposed essentially the same idea for C back in
1977.  Unfortunately, Dennis didn't think it was worth the effort :-)

> These are problems which I had not considered when writing the PEP. It
> seems to me that the best way to solve them is to choose different
> keywords. How about:

>      perform:
>          <setup code>
>      whilst <condition>:
>          <loop body>

This is obviously intended to be tongue-in-cheek.  However, it does give
me a thought about how to express this notion in a way that is much clearer
and requires no new keywords:

        while:
            <setup code>
        and while <condition>:
            <loop body>

Indeed, we can think of this usage as syntactic sugar for the following:

    while:   ==>   while True:

    <dedent> and while <condition>: <indent>   ==>   if not (<condition>): break

By implication, there is no obvious reason that one could not write

        while <condition1>:
            <code1>
        and while <condition2>:
            <code2>
        and while <condition3>:
            <code3>

which would be equivalent to

        while <condition1>:
            <code1>
            if not (<condition2>): break
            <code2>
            if not (<condition3>): break
            <code3>

Looking at these examples, I find myself liking the "and while" idea more,
because it uses (negative) indentation to mark the possible exit points.


-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list