Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Is there an easier way to break out of nesting loops than throwing an exception? (In Perl, you can give labels to each loop and at least continue an outer loop.)

for x in range(10): 

for y in range(10):

print x*y 

if x*y > 50:

"break both loops"

I.e. is there a nicer way than:

class BreakIt(Exception):

pass try:

for x in range(10):

for y in range(10):

print x*y 

if x*y > 50:

raise BreakIt 

except BreakIt:

pass

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code to break out of nested loops:-

for x in xrange(10):

for y in xrange(10):

print x*y 

if x*y > 50:

break

else:

continue 

break 

Related questions

0 votes
4 answers
+1 vote
2 answers
asked Aug 28, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...