do-while loop?

Johann Hibschman johann at
Fri Mar 24 20:16:42 CET 2000

Tom Funk writes:

> In an article posted Fri, 24 Mar 2000 02:57:44 +0000,
> Peter Bittner (bittneph at said:

>> I'm missing a 'do-while' statement in Python.

> Alx and Charles are both correct, but I recently discovered something 
> that makes this approach problematic: how to get out of nested loops.  In 
> cases where I'm nesting loops, I've taken to using something like this:

<snip niftyness>

Personally, in that case I'd use an abort flag, like

abort = 0
while not abort:
    # blah blah
    if <normal loop test>:
    for cmd, arg in commands:
        if ...:
        if <some error happens>
            abort = 1
            abort_msg = "Hey!  this loop ended with an error"
    print abort_msg

Basically, this uses the fact that a while loop exited with "break"
does not call the else clause, while one exited with a normal
while-control-test (i.e. if "not abort" is false) does.

Just something to keep in mind.  It's not as flexible as the
exception-based method, but it has its advantages.

Johann Hibschman                           johann at

More information about the Python-list mailing list