why no "do : until"?

gregory_wilson at my-deja.com gregory_wilson at my-deja.com
Wed Jan 10 11:02:45 EST 2001


> Can someone point me at an explanation of the logic for not having
> do:
> until

A proposal I floated a year ago, based on feedback from students in
the Python course I teach, was:

    do:
      first-half
    while cond:
      second half

which would allow the following degenerate forms:

    do:
      body
    while cond

and

    while cond:
      body

In the discussion that followed, I pointed out one
reason why people want this.  If you write a C-style
assignment-in-the-test 'while' loop, you can capture
the loop control in a variable for use in the loop
body:

    while (line = readline()):    # not legal Python, but useful
      do things with line

To do this in Python, you need:

    while 1:
      line = readline()
      if (some test on line): break
      do things with line

because there is no way to capture the while's
conditional test in a variable in the while
statement itself.  One of the advantages of a
test-in-the-middle loop is that it allows this:

    do:
      line = readline()
    while (some test on line):
      do things with line

The "while 1/break" idiom also allows it, but
because the 'break' isn't outdented, it isn't
as easy for readers to see where the test is
being done.

Greg


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list