do ... while loop

Tim Peters tim_one at email.msn.com
Thu Oct 14 01:11:47 EDT 1999


[Joshua Marcus, fighting initial lack-of-feature bloat <wink>]
> ...
> There are, in fact, other situations in which one actually wants a
> "do...while" loop & one is forced to either repeat code or make a
> meaningless initial assignment.

Those are never necessary, although it's amazing how often people fall into
believing they are.  The usual Python idiom for:

    do {
        xxx;
        yyy;
    } while (condition);

is simply:

    while 1:
        xxx
        yyy
        if not condition:
            break

The criminally insane sometimes write the last line instead as:

        if not condition: break

but putting it on separate lines (as God intended) is key to making the
idiom instantly (after you're used to it -- about two days) recognizable for
what it is.

> ...
> I'd be surprised if they find the 'do...while' loops confusing to read.

Has been debated many times.  There's no pleasant way to spell it in Python;
all blocks have opening keywords, but never closing keywords.

suspecting-you're-going-to-reinvent-the-last-10-proposals-anyway<wink>-ly
    y'rs  - tim






More information about the Python-list mailing list