# How to break several nested 'for' loop in one 'if'

**URL:** https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726
**Category:** General Usage
**Created:** [December 26, 2019, 8:44pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726 "2019-12-26T20:44:07Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [December 26, 2019, 8:44pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/1 "2019-12-26T20:44:07Z")

</div>

Hi community,  
Available manual and posts gave me some hints of break for ‘for’ loop. However, intending to break 2nd and 3rd ‘for’ loops and return to the 1st ‘for’ loop, two breaks don’t work here.

> s0=0.05; N=1e6;  
> violation=Int64;  
> x1 #allele frequency array
> 
> for a in 1:rows #except wild type  
> **#two breaks intend to return to ‘for a in …’**
> 
> for b in 1:col#column  
> if x1[a,b] \> 1/(2_N_s0)  
> for c in b+1:col  
> if x1[a,c]==0 #go extinction  
> push!(violation,a)  
> **break #for c in …**  
> **break #for b in …**  
> end  
> end  
> end  
> end  
> end  
> print(violation)

Thanks,  
Amend

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [December 26, 2019, 8:59pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/2 "2019-12-26T20:59:58Z")

</div>

You would need to do some other way than `break`. Possibilities are `@goto` and just setting some variable and then look at that variable in the outer loop and break there.

---

<div class="post-metadata">

### Author: ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)
#### Post date: [December 26, 2019, 9:33pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/3 "2019-12-26T21:33:41Z")

</div>

A reply along the same line was recently given [here](https://discourse.julialang.org/t/nested-functions-pros-and-cons/19417/24).

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 26, 2019, 9:47pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/4 "2019-12-26T21:47:25Z")

</div>

Related reading: [https://github.com/JuliaLang/julia/issues/5334](https://github.com/JuliaLang/julia/issues/5334)

---

<div class="post-metadata">

### Author: ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)
#### Post date: [December 27, 2019, 4:10am UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/5 "2019-12-27T04:10:05Z")

</div>

You can write it as a function and return.

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [December 27, 2019, 11:05pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/7 "2019-12-27T23:05:58Z")

</div>

I see! This works.

> (function()  
> for i = 1:100  
> for j = 1:100  
> return  
> end  
> end  
> end)()

Codes as above is from the link [`break` and `continue` out of multiple loops · Issue #5334 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5334) suggested by @GunnarFarneback. Thank you~

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [December 28, 2019, 12:05am UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/8 "2019-12-28T00:05:08Z")

</div>

Thank you. This works.

> for i=1:3  
> for j=1:3  
> for k=1:3  
> @info “indices” (i, j, k)  
> if k == 2  
> @goto end\_j  
> end  
> end  
> end  
> @label end\_j  
> end

Codes quoted from “@c42f” on [`break` and `continue` out of multiple loops · Issue #5334 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5334#issuecomment-39616744)

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [December 28, 2019, 4:39pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/9 "2019-12-28T16:39:04Z")

</div>

I decided to turn my gist from [https://github.com/JuliaLang/julia/issues/5334#issuecomment-206508859](https://github.com/JuliaLang/julia/issues/5334#issuecomment-206508859) into a [package](https://github.com/GunnarFarneback/Multibreak.jl). If you do

```julia
using Pkg; Pkg.add("https://github.com/GunnarFarneback/Multibreak.jl")

```

you should be able to `break` twice as you wanted with help from the `@multibreak` macro. See the [tutorial](https://github.com/GunnarFarneback/Multibreak.jl/blob/master/test/tutorial.jl) for documentation.

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [December 28, 2019, 5:18pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/10 "2019-12-28T17:18:19Z")

</div>

This is exactly what I am looking for. Thanks a lot!

> using Pkg; Pkg.add(“[GitHub - GunnarFarneback/Multibreak.jl: Multibreak provides a macro to allow breaking out of several loops at once in Julia code.](https://github.com/GunnarFarneback/Multibreak.jl)”)  
> using Multibreak  
> out =   
> @multibreak for i = 1:5  
> for j = 1:3  
> if i == 1 && j == 1  
> continue  
> elseif i == 2 && j == 2  
> break  
> elseif i == 3 && j == 2  
> break; continue  
> elseif i == 4 && j == 2  
> break; break  
> end  
> push!(out, (i, j))  
> end  
> push!(out, i)  
> end
> 
> ```
> @test out == [(1, 2), (1, 3), 1,
> (2, 1), 2,
> (3, 1),
> (4, 1)]
> 
> ```
> 
> end

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [December 28, 2019, 6:54pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/11 "2019-12-28T18:54:58Z")

</div>

I wonder if it’s worthwhile to make a macro to break to specific label?

```julia
@mark a for i in I
    for j in J
        ...
        @break a
    end
end

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 29, 2019, 7:08am UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/12 "2019-12-29T07:08:49Z")

</div>

It would be great to have something like [`common-lisp:return-from`](http://clhs.lisp.se/Body/s_ret_fr.htm), ie a named block that would return that value when return-from is invoked inside.

I am not sure if it can be done with a macro in a compiler-friendly manner though.

---

<div class="post-metadata">

### Author: ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)
#### Post date: [December 29, 2019, 8:54am UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/13 "2019-12-29T08:54:21Z")

</div>

I bet we can have this:( **EDIT: with only macro** )

```julia
a = 1
b = 2
@mark label=l outer_vars=[a] for i in I
     a = 11
     b = 22
     @break l
end
# a = 11
# b = 2

```

We just cannot share scopes automatically.

---

<div class="post-metadata">

### Author: ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)
#### Post date: [December 30, 2019, 10:42pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/14 "2019-12-30T22:42:20Z")

</div>

Just for my interest to ask, what’s the difference between ‘@multibreak begin end’ (I thought it functions by introducing a macro?) and a made macro ‘@mark @break’ as you suggested?

Is the bad reputation of ‘@goto @label’ the reason of a special call ‘@break’?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [January 5, 2020, 10:17am UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/15 "2020-01-05T10:17:31Z")

</div>

> [@GunnarFarneback](#):
>
> I decided to turn my gist from [https://github.com/JuliaLang/julia/issues/5334#issuecomment-206508859](https://github.com/JuliaLang/julia/issues/5334#issuecomment-206508859) into a [package](https://github.com/GunnarFarneback/Multibreak.jl). If you do
> 
> ```julia
> using Pkg; Pkg.add("https://github.com/GunnarFarneback/Multibreak.jl")
> 
> ```

This is now a registered package, so no need to add it by URL any longer.

> [@1634](#):
>
> Just for my interest to ask, what’s the difference between `@multibreak begin end` (I thought it functions by introducing a macro?) and a made macro `@mark @break` as you suggested?

In my opinion the main difference is that `@multibreak` doesn’t require coming up with any additional names for loops or labels. Having names may make things clearer if the nesting is very deep or complicated but let’s not fool ourselves by the generality of the construction. I expect that 90% of the use cases for `@multibreak` is `break; break`, 9% `break; continue` and at most a small fraction goes any deeper. In the rare cases where the loops are so convoluted that `@multibreak` control flow would be hard to follow I would just use the existing `@goto` and `@label` macros.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 3, 2021, 9:01pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/16 "2021-05-03T21:01:40Z")

</div>

Had same problem as OP to exit from two nested loops and easiest way seems to be to use the `@goto` and `@label` macros as suggested above by @kristoffer.carlsson.

Could not find much information on the Julia manual about this and would like to ask if this is a common (/recommended) practice in Julia language?

Thank you.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 3, 2021, 9:16pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/17 "2021-05-03T21:16:33Z")

</div>

> [@rafael.guerra](#):
>
> if this is a common (/recommended) practice

It is a common and recommended practice in Fortran77 and older 🙂 . Since then the recommended is to avoid having to to that in some whay that the code keeps modular. (of course, that is only important when anyone will ever have to read and follow your code…)

(jokes apart, there are many cases where it is so much more natural to do that, that, you can see, workarounds still abound).

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [May 3, 2021, 9:30pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/18 "2021-05-03T21:30:42Z")

</div>

I use the Multibreak package every time I need to break out of two loops, but I may be biased.

I suspect using a helper variable for the control flow is the most common solution, unless it’s easy to place the loops in a subfunction you can return from or you can loop over multiple variables in a single for loop (in which case a single `break` suffices). I rarely see the `@goto` and `@label` macros in the wild and when I’ve seen them it has been for more complex control flow than just breaking out of two loops.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 3, 2021, 11:28pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/19 "2021-05-03T23:28:20Z")

</div>

@GunnarFarneback, thank you for your valuable answer.

You made me rethink the program and improve it.  
Reorganizing the code and using a `while` inner loop and a helper variable instead of the `for` loop, was enough to get rid of the `@goto` macro.  
There should be a good reason why this macro does not appear in Julia’s manual [Control Flow](https://docs.julialang.org/en/v1/manual/control-flow/) section.

Regarding your nice Multibreak.jl package, the [tutorial documentation](https://github.com/GunnarFarneback/Multibreak.jl/blob/master/test/tutorial.jl) scared the hell out me:

“_A word of warning. The `@multibreak` macro also transforms single `break` and `continue` to `@goto` and `@label`. A side effect of the implementation is that **dead code can come alive**._”

(It sounds like the return of the living dead! 👻)

Thanks again 🙂

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 3, 2021, 11:57pm UTC](https://discourse.julialang.org/t/how-to-break-several-nested-for-loop-in-one-if/32726/20 "2021-05-03T23:57:46Z")

</div>

> [@rafael.guerra](#):
>
> There should be a good reason why this macro does not appear i

There is some truth in my joke above. The criticisms certainly may not apply to a very specific and local use of labels and go-tos, but in general it is a code layout that is better avoided, because it makes sections of the code hard to understand and reuse.

[Goto - Wikipedia](https://en.m.wikipedia.org/wiki/Goto#:~:text=GoTo%20)(goto%2C%20GOTO%2C%20GO,function%20call%20normally%20returns%20control.
