[ python-Feature Requests-1210975 ] "break" and "continue"-ing out of nested 'for' loops

SourceForge.net noreply at sourceforge.net
Mon May 30 03:43:03 CEST 2005


Feature Requests item #1210975, was opened at 2005-05-29 17:59
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1210975&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: None
>Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Geraint Luff (geraintluff)
Assigned to: Nobody/Anonymous (nobody)
Summary: "break" and "continue"-ing out of nested 'for' loops

Initial Comment:
I wasn't entirely sure whether this should go under
"Interpreter Core" or "Parser/Compiler", but whatever.

At the moment, "break" and "continue" can only break
out of the innermost "for" loop. I would appreciate a
way of breaking out of multiple 'for' or 'while' loops.

This would be extremely useful for instance when you
have some 'for' loops cycling through several
combinations, and you are using another 'for' loop to
check their validity (in my example, checking that my
two values are co-prime). In this situation, the only
solution I have found is to create a boolean variable
that is assigned a value when the check fails, just
before 'break'-ing out of a 'for' loop, and immediately
after that 'for' loop, having a statement that checks
the status of the boolean variable, and then
'continue's or 'breaks' again accordingly. I'm not an
experienced programmer, but this strikes me as
inefficient :p

Sorry if the above explanation is muddled. I can send a
short (15 lines) bit of code which might explain it better.

BTW, PHP seems to do this with "break 2;" or "continue
2;" which is very useful.

--G

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-05-29 20:43

Message:
Logged In: YES 
user_id=80475

Sorry, this was considered and rejected long ago.  Most
other languages have made a similar choice.  In Python, the
best alternatives include:

* try/except which can penetrate multiple layers of looping
and/or function calls.

* boolean flags

* enclosing the innermost loops in a function so that a
return statement exits all pending loops within the function.

*and try/finally statements which are not a general purpose
replacement but do cover a whole class of use cases.

For more reading on the subject, take a look at Knuth's
Structured Programming with go to Statements.  That is a
rather thorough analysis of various structured constructs
and their strengths and weaknesses.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1210975&group_id=5470


More information about the Python-bugs-list mailing list