Is there a way to break multiple loops?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

If I have layered for loops. How can I break all of them in one solve?
(Simple example below, that I’d like to use in a more complex setting.)

for x in 10:
	if x = 5:
		for i in 3:
			if i > 1:
				for a in 15:
					if a = 7:
						break

How can I break all three loops once a = 7? Is this even possible?

:bust_in_silhouette: Reply From: cascas

Not sure if this is what you’re looking for but you could put all loops into a function and return the function.

:bust_in_silhouette: Reply From: exuin

I think you should use a function, but another way would to be to have a boolean variable that is checked at the start of every loop that you would set to false when you want to break the loop. But that would require n-1 variables for n nested loops.

Sorry, I noticed I never gave credit for this answer. Here is my long overdue thank you!

Dumuz | 2021-06-04 16:12

haha it’s fine i answer a lot of questions

exuin | 2021-06-04 16:14