PEP 315: Enhanced While Loop

Ben Allfree benles at bldigital.com
Sat May 3 10:53:43 EDT 2003


At least one other person sees the sense in what I was suggesting :)

At one point, quite a few BASIC and Pascal flavors had a repeat..until or
do..while loop construct. It seems that it faded away, and I'm not sure why.

"posttest while" is as good as anything. It introduces only one key word, is
clear, and doesn't force the looping construct to be split into separate
blocks. Part of the elegance of Python is that the indentation level tells a
lot about the execution path. The do/while PEP would break that rule.

> -----Original Message-----
> From: python-list-admin at python.org [mailto:python-list-admin at python.org]
> On Behalf Of Dan Bishop
> Sent: Saturday, May 03, 2003 2:37 AM
> To: python-list at python.org
> Subject: Re: PEP 315: Enhanced While Loop
> 
> "Ben Allfree" <benles at bldigital.com> wrote in message
> news:<mailman.1051935786.17713.python-list at python.org>...
> > Can you provide an example snippit?
> >
> > I'm interested, but I can't come up with a scenario where the loop
> couldn't
> > be rewritten, except for do..until loops that must execute at least
> once,
> > which require the first loop iteration to be placed before a while loop.
> 
> After grepping through a few thousand lines of Python code, I found
> only one use of the "while 1"..."break" construct (which I assume is
> what do...while intends to replace), which was the main loop in my
> text adventure game.
> 
>    while 1:
>       # ...
>       if not getYN("You are now dead.  Would you like to play again
> (Y/N)?"):
>          break
>       if getYN("Would you like to read the intro again (Y/N)?"):
>          intro()
> 
> I'm not sure what the equivalent do...while loop would be.
> 
> > And for that scenario, I would propose a simple "until" loop construct
> that
> > is guaranteed to execute once before evaluation:
> >
> > # Old way
> > MakeMoney()
> > while Poor():
> >    MakeMoney()
> >
> > # New way
> > until not Poor():
> >    MakeMoney()
> >
> > The above example assumes one must MakeMoney() before running the Poor()
> > test ;)
> >
> > Python programmers would just have to understand that the "until"
> construct
> > executes once before evaluation. Or maybe it could be named something
> more
> > obvious. "AfterWhile" - heh - no pun intended.
> 
> TI-8x BASIC (the only language I know of that puts posttest loop
> conditions at the beginning) uses
> 
> :Repeat not Poor()
> :MakeMoney
> :End
> 
> (Actually, this isn't valid code, because there are no user-defined
> functions and no identifiers longer than 8 chars, but this is
> irrelevant.)
> 
> In future Python, this could be written
> 
> repeat not Poor():
>    MakeMoney()
> 
> My personal preferred syntax is
> 
> posttest while Poor():
>    MakeMoney()
> --
> http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list