No Do while/repeat until looping construct in python?

John Roth johnroth at ameritech.net
Fri Mar 14 09:35:28 EST 2003


"Greg Ewing (using news.cis.dfn.de)" <me at privacy.net> wrote in message
news:b4rkcq$22tehe$1 at ID-169208.news.dfncis.de...
> Alex Martelli wrote:
> > Yes.  Having a single loop construct as Knuth suggested many decades
> > ago, e.g.:
> >
> >     repeat:
> >         <pre-code>
> >     while <test>:
> >         <post-code>
> >
> > might be nicer syntax sugar than the current Python approach:
> >
> >     while True:
> >         <pre-code>
> >         if not <test>: break
> >         <post-code>
> >
> > but the syntax-sugar difference is small enough to make it no
> > big deal, anyway.
>
> I think it's a somewhat bigger deal than you make out,
> since it would bring the exit condition into a prominently
> visible place, instead of being buried and easy to miss
> at first glance.

That's an interesting point, but I wouldn't like to use "while."
It's way too easy to confuse with the beginnings of a loop.

However, I think the figure is more likely to be:

repeat:
        <pre-code>
    while <contition>:
        <post-code>

That clearly expresses what is subordinate to what,
although it would break the strict definition of how
indentation is supposed to work.

> And this is *such* a common looping pattern that I really
> don't understand why *no* language I can remember seeing
> has explicit support for it.

Well, I suspect that part of the reason is that a lot of language
designers simply follow the leader with "continue" and "break."
Another part of the reason is that all those silly braces in most
languages just obscure the issue.

And notice the problems that novices get into with else clauses
in languages that treat 'else' as a full statement with restrictions
on where it can be placed.

John Roth

>
> --
> Greg Ewing, Computer Science Dept,
> University of Canterbury,
> Christchurch, New Zealand
> http://www.cosc.canterbury.ac.nz/~greg
>






More information about the Python-list mailing list