# How to break out of exactly the current loop?

**URL:** https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457
**Category:** General Usage
**Tags:** question, jump
**Created:** [April 30, 2020, 12:10am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457 "2020-04-30T00:10:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)
#### Post date: [April 30, 2020, 12:10am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457/1 "2020-04-30T00:10:30Z")

</div>

Hi all, I know the use of break, which is that a ‘break’ statement inside such a loop exits the entire nest of loops, not just the inner one. What if I just want to exit the inner one?  
For instance

```julia
for i in 1:5, j in 1:10
       println(i)
       println(j)
       if j == 5
           break
       else 
           continue
       end
end

```

The upper is a nested loop. How to break the inner loop only? Thanks!

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 12:13am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457/2 "2020-04-30T00:13:39Z")

</div>

Just separate it out into two for loops:

```julia
for i in 1:5
    for j in 1:10
        println(i)
        println(j)
        if j == 5
            break
        else 
            continue
        end
    end
end

```

---

<div class="post-metadata">

### Author: ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)
#### Post date: [April 30, 2020, 12:24am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457/3 "2020-04-30T00:24:09Z")

</div>

> [@Mason](#):
>
> for i in 1:5 for j in 1:10 println(i) println(j) if j == 5 break else continue end end end

Thank you for your reply. However, if we consider the following code,

```julia
while true
	checkArray = zeros(0)
       #the for-loop will run three times
	for j in 1:3
		#do something to get the result
		result = solve(function1, function2)
		global result
		push!(checkArray, length(result))
	end

	#if the elements in checkArray are not the same, the while-loop will run again.
	if length(unique(checkArray)) == 1
		break
	else 
		continue
    end
	print("hello")
end

```

‘hello’ will never be output if length(unique(checkArray)) == 1.  
Could you please help me solve this?

---

<div class="post-metadata">

### Author: ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)
#### Post date: [April 30, 2020, 12:48am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457/4 "2020-04-30T00:48:14Z")

</div>

Looks like you should put the `break` check inside the `j` loop if that’s the loop you want to break out of.

---

<div class="post-metadata">

### Author: ![Yusen\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yusen_liu/32/13760_2.png) [@Yusen\_Liu](https://discourse.julialang.org/u/Yusen_Liu)
#### Post date: [April 30, 2020, 12:58am UTC](https://discourse.julialang.org/t/how-to-break-out-of-exactly-the-current-loop/38457/5 "2020-04-30T00:58:29Z")

</div>

Oh yeah! Thanks a lot.
