# How use break in loop if ? ERROR: syntax: break or continue outside loop

**URL:** <https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307>\
**Category:** General Usage\
**Created:** [May 17, 2019, 5:39am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307 "2019-05-17T05:39:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [May 17, 2019, 5:39am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/1 "2019-05-17T05:39:56Z")

</div>

```julia
julia> if 1>0 println("Stop") ;break
       else println("Go")
       end
ERROR: syntax: break or continue outside loop

```

Thanks  
Paul

---

<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 17, 2019, 6:04am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/2 "2019-05-17T06:04:28Z")

</div>

An `if` statement is not a loop. `for` and `while` statements are loops and can be used with `break` and `continue`.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [May 17, 2019, 7:01am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/3 "2019-05-17T07:01:43Z")

</div>

Yes, but 1.st loop run and run

```julia
while true
println(1)
     while 1>0 println("Stop"); break
     end
println(2)
end
1
Stop
2
1
Stop
2
1
Stop
...

```

I need to break 1.st lopp ! like this :

```julia
while true
println(1)
     if 1>0 println("Stop"); break
     end
println(2)
end
1
Stop

julia>

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 17, 2019, 8:05am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/4 "2019-05-17T08:05:44Z")

</div>

You can use [short-circuit evaluation](https://docs.julialang.org/en/v1/manual/control-flow/index.html#Short-Circuit-Evaluation-1), this works:

```julia
while true
    println(1)
    1>0 && begin
        println("stop") 
        break
    end
println(2)
end

```

---

<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 17, 2019, 8:15am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/5 "2019-05-17T08:15:46Z")

</div>

> [@programista](#):
>
> I need to break 1.st lopp ! like this :
> 
> ```julia
> while true
> println(1)
> if 1>0 println("Stop"); break
> end
> println(2)
> end
> 1
> Stop
> 
> ```

If you need to break it like that I suggest you do exactly that. The error in your original post happens if you try to run the `if` and `break` in isolation without the `while` loop context, which isn’t a meaningful thing to do.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [May 17, 2019, 10:11am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/6 "2019-05-17T10:11:31Z")

</div>

but, but, but… How to break loop for without while inside?

```julia
for i=1:10
println(1)
do somethink
     if 1>0 println("Stop"); break
     end
println(2)
do more...
end

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 17, 2019, 10:18am UTC](https://discourse.julialang.org/t/how-use-break-in-loop-if-error-syntax-break-or-continue-outside-loop/24307/7 "2019-05-17T10:18:34Z")

</div>

> [@programista](#):
>
> but, but, but… How to break loop for without while inside?

Just like you wrote:

```julia
julia> for i=1:10
       println(1)
       #do somethink
            if 1>0 println("Stop"); break
            end
       println(2)
       #do more...
       end
1
Stop

```

Or do you want

```julia
       println(1)
       #do somethink
            if 1>0 println("Stop"); continue
            end
       println(2)
       #do more...
       end
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop
1
Stop

```
