Breaking out of nested loop

Phlip phlip_cpp at my-deja.com
Tue Feb 27 11:01:42 EST 2001


Proclaimed Dan from the mountaintops:

> 
> How might one go about breaking out of a multi level loop?
> 
> For instance, the following code will go into an endless loop.
> 
> while(1):
>     while(1):
>         break

They covered this in a recent thread.

Your best bet (just like in a language that has a 'goto') is 'return'.

Otherwiset you can 'raise' (irritating style that reminds one of VB), or 
you can add a flag to the 'while' conditions and reset it at the 'break' 
point.

Following these style rules...

        simplify 'if' and 'while' test conditions

        don't 'raise' except in an emergency

        shun long functions - don't go over 30 lines or 2 loop statements

...you'l see the best fit is 'return'.

-- 
  Phlip                          phlip_cpp at my-deja.com
============== http://phlip.webjump.com ==============
  --  Spaghetti Bad. Re-use Good.  --



More information about the Python-list mailing list