No, loop-and-a-half! (Re: REPEAT... UNTIL ?)

Paul Svensson paul at svensson.org
Sun Jul 14 05:36:47 EDT 2002


bellman at lysator.liu.se (Thomas Bellman) writes:

>Greg Ewing <see_reply_address at something.invalid> wrote:
>
>> On the other hand, a situation that *is* very common is
>> a loop-and-a-half, with the exit condition in the middle.
>> So far, I've never seen *any* really good loop-and-a-half
>> structure in any language, and I think Python has a chance
>> to be truly innovative here.
>
>As others have already said, iterators can sometimes alleviate
>this problem.
>
>And there is *one* language where I think the structure for
>loop-and-a-half *is* good: Forth.  The syntax goes something
>like

 (---)

Bourne Shell also has

        while
                foo
                bar
                gazonk ?
        do
                gurka
        done

>I *would* have liked that to be
>
>    repeat:
>       part_1()
>    while test_1():
>       part_2()
>    while test_2():
>       part_3()
>    while test_3():
>       part_4()
>
>in Python, but that is unfortunately not compatible with the
>current Python syntax. :-(

The problem is that there's no indication where the repeat: block ends.
If we should invent new syntax, I would limit it to the loop-and-a-half,
and keep "break" for multiple exit loops.

    repeat:
        part_1()
    while test_1():
        part_2()

This is unambigous to the compiler, but not could be confusing to humans,
specially if part_1() is large.  Adding more new keywords makes it clearer:

    repeat:
        part_1()
    until not test_1():
        part_2()

Or, inspired by Bourne, with no new keywordss at all (and even more cryptic):

    while:
        part_1()
        test_1():
            part_2()

I don't see anything like this happening anytime soon.

        /Paul



More information about the Python-list mailing list