How to label loops?

Steffen Ries steffen.ries at sympatico.ca
Sun Feb 11 20:35:53 CET 2001


Zamurai <zamurai at gmx.net> writes:

> "Steve Holden" <sholden at holdenweb.com> wrote:
> > First, convince me that loops need labelling! 
... 
> 
> for example:
> 
> while 1:                # first loop
>     while 1:            # second loop 
>         break
> 
> 
> this break breaks only out of the first loop, but what, if i want to
> break out of the first loop, while i´m in the second one?

one way is to use control variables, e.g.:

outer_active = 1
inner_active = 1
while outer_active:
    while inner_active:
       outer_active = 0
       break

another way would be to use exceptions, e.g.:

try:
    while 1:
        while 1:
            raise Exception, "broken loop"
except Exception:
    pass

/steffen
-- 
steffen.ries at sympatico.ca	<> Gravity is a myth -- the Earth sucks!



More information about the Python-list mailing list