do-while loop?
Johann Hibschman
johann at physics.berkeley.edu
Fri Mar 24 14:16:42 EST 2000
Tom Funk writes:
> In an article posted Fri, 24 Mar 2000 02:57:44 +0000,
> Peter Bittner (bittneph at aston.ac.uk) 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>:
break
for cmd, arg in commands:
if ...:
...
if <some error happens>
abort = 1
abort_msg = "Hey! this loop ended with an error"
else:
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 physics.berkeley.edu
More information about the Python-list
mailing list