[Python-ideas] Updating PEP 315: do-while loops

Ron Adam rrr at ronadam.com
Sun Apr 26 04:50:52 CEST 2009

Raymond Hettinger wrote:

> 2) Another approach is to put the test at the end.  Something like:
>    do:
>        j = _int(random() * n)
>    :while j in selected
>    do:
>        j = _int(random() * n)
>        while j in selected

> These seem syntactically weird to me and feel more like typos than real python code.
> I'm sure there are many ways to spell the last line, but in the two years since I first worked
> on the PEP, I haven't found any condition-at-the-end syntax that FeelsRight(tm). 

I think having the "while" start some blocks but end others is very weird 
indeed and makes it harder to keep stuff in my head.

I'd prefer a bare "while:" with a more visible "break if" and possibly 
"continue if" expressions.

Visualize the while, continue, and break with syntax highlighting.

          j = _int(random() 8 n)
          break if j not in selected

Or if the reverse comparison is preferred you could do...

          j = _int(random() * n)
          continue if j in selected

Or we could allow break and continue in the if-else expression syntax ...

          continue if (j in selected) else break


          break if (j not in selected) else continue

In this last case the final 'else continue' can be dropped off sense it 
would be redundant:

          break if (j not in selected)

That would give us back the first example again.

By moving the break and continue out from under the if statement it makes 
it more visible and follows a common pattern of having lines start with 
keywords, which is very readable with syntax highlighting.  It also still 
allows the break to be anywhere in the body which is more flexible.

Because it is vary close to the current while syntax, is there really any 
need to rename the bare 'while' to 'do'?   I just think of it as 'do for a 
while' or short for 'while True'.


More information about the Python-ideas mailing list