# Add \`if\` to for-loops like in comprehensions

**URL:** https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632
**Category:** Internals & Design
**Created:** [January 21, 2020, 6:53pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632 "2020-01-21T18:53:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)
#### Post date: [January 21, 2020, 6:53pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/1 "2020-01-21T18:53:27Z")

</div>

apologies if this has been discussed before, but i was surprised it didn’t work when i naively tried it:

```julia
julia> for i in 1:10 if i>3
       @info i
       end

ERROR: syntax: incomplete: "for" at REPL[1]:1 requires end
Stacktrace:
 [1] top-level scope at REPL[1]:0

```

strange because the `if` clause works in comprehensions:

```julia
julia> [i for i in 1:10 if i>3]
7-element Array{Int64,1}:
  4
  5
  6
  7
  8
  9
 10

```

has there been any discussion on making this possible? why wouldn’t we want them to work similarly?

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [January 21, 2020, 7:09pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/2 "2020-01-21T19:09:00Z")

</div>

One way you could do it is with a generator.

```julia
foreach( (i for i in 1:10 if i>3) ) do i
    @info i
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: [January 21, 2020, 7:13pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/3 "2020-01-21T19:13:12Z")

</div>

Or just (well, mostly) pressing return.

```julia
for i in 1:10
    if i>3
        @info i
    end
end

```

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [January 21, 2020, 7:19pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/4 "2020-01-21T19:19:09Z")

</div>

You could probably put the logic inside the range like

```julia
for i in filter(x -> x > 3,1:10)
@info i
end

```

(I didn’t test this)

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [January 21, 2020, 7:32pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/5 "2020-01-21T19:32:27Z")

</div>

even more straight forward…

```julia
for i in 4:10
    @info i
end 

```

? 😁

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [January 21, 2020, 7:55pm UTC](https://discourse.julialang.org/t/add-if-to-for-loops-like-in-comprehensions/33632/6 "2020-01-21T19:55:07Z")

</div>

> [@bjarthur](#):
>
> ```
> for i in 1:10 if I>3
> @info I
> end
> 
> ```

This syntax already means something:

```julia
julia> for i in 1:6 if i > 4
           @info i
       end end
[ Info: 5
[ Info: 6

```

In other words, you just need to add a second `end` to close the `if` block. Changing this would be a breaking change to where these blocks end.
