# For loop condition filter

**URL:** <https://discourse.julialang.org/t/for-loop-condition-filter/46044>\
**Category:** General Usage\
**Created:** [September 4, 2020, 7:43am UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044 "2020-09-04T07:43:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 4, 2020, 7:43am UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044/1 "2020-09-04T07:43:03Z")

</div>

Has anybody wanted something like this?

```julia
for x in 1:10 if x != 5
    println(x)
end

```

Here’s some less-than-ideal ways:

```julia
for x in (x for x in 1:10 if x != 5)
    println(x)
end

```

```julia
for x in 1:10
    x == 5 && continue
    println(x)
end

```

```julia
for x in filter(!=(5), 1:10)
    println(x)
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:** [September 4, 2020, 7:57am UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044/2 "2020-09-04T07:57:20Z")

</div>

Here is the ideal way:

```julia
for x in 1:10
    x != 5 && println(x)
end

```

You can write out an explicit `if` if you prefer.

Extra syntax to stuff this into the loop is probably unwarranted.

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [September 4, 2020, 8:27am UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044/3 "2020-09-04T08:27:46Z")

</div>

```julia
julia> println.(x for x in 1:10 if x != 5)

```

or for nicer REPL display

```julia
julia> println.(x for x in 1:10 if x != 5);

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [September 4, 2020, 11:19am UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044/4 "2020-09-04T11:19:09Z")

</div>

And maybe a remark about the background:  
In my view an iterator is an abstraction of a loop people might write. E.g. `(x for x in 1:10 if x != 5)` _is_ the abstraction of writing a loop

```julia
for x in 1:10
    if x != 5
        ....
    end
end

```

but it does not make the vanilla loop obsolete or replace it.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 4, 2020, 12:32pm UTC](https://discourse.julialang.org/t/for-loop-condition-filter/46044/5 "2020-09-04T12:32:28Z")

</div>

Perhaps just personal preference, but I usually prefer to write this as

```julia
for x in 1:10
    x != 5 || continue
    ....
end

```

since it avoids an extra level of nesting.
