[Python-Dev] PEP 351 - do while

Andrew Koenig ark at acm.org
Sun Oct 1 20:44:55 CEST 2006


> This pattern:
> 
>       while entry_cond:
>          ...
>       and while not exit_cond:
>          ...
> 
> has been suggested before, and I believe that at least one of the times it
> was suggested, it had some support from Guido.  Essentially, the "and
> while not exit" is equivalent to an "if exit: break" that's more visible
> due to not being indented.

I like this suggestion.  In fact it is possible that at one time I suggested
something similar.

It reminds me of something that Dijkstra suggested in his 1971 book "A
Discipline of Programming."  His ides looked somewhat like this:

	do condition 1 -> action 1

	...

	[] condition n -> action n
	od

Here, the [] should be thought of as a delimiter; it was typeset as a tall
narrow rectangle.

The semantics are as follows:

	If all of the conditions are false, the statement does nothing.

	Otherwise, the implementation picks one of the true conditions,
	executes the corresponding action, and does it all again.

There is no guarantee about which action is executed if more than one of the
conditions is true.

The general idea, then, is that each action should falsify its corresponding
condition while bring the loop closer to termination; when all of the
conditions are false, the loop is done.

For example, he might write Euclid's algorithm this way:

	do x < y -> y := y mod x
	[] y < x -> x := x mod y
	od

If we were to adopt "while ... and while" in Python, then Dijkstra's
construct could be rendered this way:

	while x < y:
		y %= x
	or while y < x:
		x %= y

I'm not suggesting this seriously as I don't have enough realistic use
cases.  Still, it's interesting to see that someone else has grappled with a
similar problem.





More information about the Python-Dev mailing list