Python Forum
breaking out of nested loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: breaking out of nested loops (/thread-37755.html)



breaking out of nested loops - Skaperen - Jul-17-2022

deep in some nested loops and a condition test, i need to break out of all of those loops. is there an easy way to do this? i can't use goto. what about a faked try/except?


RE: breaking out of nested loops - jefsummers - Jul-17-2022

Using an exception will work, but is a kludge and can end up causing bigger problems down the road, esp with maintenance.
Another, again not great idea, is to use a flag that you test as you exit the deeper loops to see if you should break

I'd look at how you could restructure your code


RE: breaking out of nested loops - Pedroski55 - Jul-18-2022

Maybe put your loops in a function?

if condition:
return

should beam you out of the function!


RE: breaking out of nested loops - Skaperen - Jul-18-2022

i like the function idea. that would better organize my code, too.