On 04/10/2017 11:42, Stefan Ram wrote:
Robin Becker <ro...@reportlab.com> writes:
Given the prevalence of the loop and a half idea in python I wonder why we don't
have a "do" or "loop" statement to start loops without a test.

   VBA has quite an orthogonal way to build loop control:

   pre-checked positive:

Do While ...
     ...
Loop

   post-checked positive:

Do
     ...
Loop While ...

   pre-checked negative:

Do Until ...
     ...
Loop

   post-checked negative

Do
     ...
Loop Until ...

   "endless":

Do
     ...
Loop

None of them appear to be loop-and-a-halves.

(Note that your reverse-indentation style is confusing! Imagine that Python was written like this:

    for i in R:
print (i)
)

Anyway I think what is needed in Python for a proper loop-and-a-half is this:

  while:
     s1
     s2
     do cond:
       s3
       s4

s1, s2, s3, s4 are executed in sequence while cond is true. When cond is false, only s1, s2 are executed in that iteration, and the loop terminates. (A few syntactical details to be worked out such as whether while, do and else line up or not.)

This is equivalent I think to the current:

  while 1:
     s1
     s2
     if cond:
       s3
       s4
     else:
       break

But this would be a less satisfactory, ad-hoc approach. For example, there can be several such if-blocks within the same loop. They could be nested. There might not be any, so can't at a glance distinguish an endless loop from a loop-and-a-half.

My proposed while-do has a less chaotic structure. It wouldn't preclude 'break' from still being used, but there would be less need.

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to