Breaking out of nested loops

Ulf Magnusson ulf.magnusson at ubm-computing.com
Sat Jan 12 09:55:24 EST 2002


"Tal Linzen" <pachy at 012.net.il> wrote in message
news:mailman.1010834178.14290.python-list at python.org...
> Good day.
>
> One feature I miss in python is the ability to break out of nested loops.
> I've searched the newsgroup archives, and found a few threads discussing
the
> problem, with no apparent conclusion. The two solutions I've seen --
raising
> an exception, or putting the loop inside a function and using the return
> statement -- are unintuitive (meaning, I didn't come up with them myself
> :-) ).
>
> Know if anyone has drafted a PEP for a syntactic solution for this
problem?
>
> Tal

I guess you mean something like this
******************
for i in ...:
    for j in ....:
         if some_condition:
                magic_break
code continues here...
******************

There are not supposed to be such a magical break, to my knowledge no
other language support this either. It kind of breaks the good behaviour you
expect from a imparative programming language.

The best solution is to set a flag inside the inner for loop and check for
that
the first thing in the outer for loop. Exceptions should only be used for
real exceptions when something is wrong, not to change the flow of the
program.

But this is of course my humble opinion

Do I make any sence?

/U. Magnusson





More information about the Python-list mailing list