# For .... if ... syntax?

**URL:** <https://discourse.julialang.org/t/for-if-syntax/64559>\
**Category:** Internals & Design\
**Tags:** loops, control-flow\
**Created:** [July 13, 2021, 12:03pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559 "2021-07-13T12:03:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)\
**Post date:** [July 13, 2021, 12:03pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/1 "2021-07-13T12:03:36Z")

</div>

I was just wondering what thoughts people have on the following syntax

```julia
for iter if cond
    #stuff here
end

```

which would basically be short for

```julia
for iter
    if cond
        #stuff here
    end
end

```

Obviously this isn’t necessary, but I don’t think it would hurt and it should be intuitive and “julian” since we already have this sort of thing for comprehensions, where something like `[i for i = 1:10 if i%2 == 0]` will return `[2,4,6,8,10]`.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 13, 2021, 12:16pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/2 "2021-07-13T12:16:23Z")

</div>

This would be a breaking change, since

```julia
for iter if cond
        # stuff
    end
end

```

is already valid syntax.

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 13, 2021, 12:41pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/3 "2021-07-13T12:41:21Z")

</div>

How about

```julia
for iter where cond
        # stuff
    end
end

```

?

`where` could be context-appropriate and is already a keyword.

---

<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:** [July 13, 2021, 12:54pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/4 "2021-07-13T12:54:50Z")

</div>

> [@dstarerstor](#):
>
> this isn’t necessary, but I don’t think it would hurt and it should be intuitive and “julian”

Language design can of course be very subjective, but I think that adding basic syntax that is not necessary for a special case of something we already have is as anti-Julian as it gets.

We have perfectly fine, composable control flow building blocks for this purpose already. Alternatives include

```julia
for i in itr
    ...
    cond || continue
    ...
end

```

depending on what each `...` does.

---

<div class="post-metadata">

**Author:** ![dstarerstor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dstarerstor/32/8958_2.png) [@dstarerstor](https://discourse.julialang.org/u/dstarerstor)\
**Post date:** [July 13, 2021, 1:06pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/5 "2021-07-13T13:06:24Z")

</div>

Personally, I think making `for` loops mirror comprehensions is pretty Julian. If we follow your logic, we should get rid of the conditional in the comprehension syntax.

`[f(i) for iter if cond(i)]`

doesn’t do anything that we can’t achieve with using `filter` or boolean indexing.

Aren’t comprehensions just shortcuts for writing a loop anyway? Maybe cut them out all together.

---

<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:** [July 13, 2021, 1:18pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/6 "2021-07-13T13:18:29Z")

</div>

> [@dstarerstor](#):
>
> doesn’t do anything that we can’t achieve with using `filter` or boolean indexing.

Neither is not a good comparison. Using the `[... for ... in ... if ...]` syntax avoids creating temporaries for the unfiltered interim results, boolean indexing OTOH would require them. `filter` operates on existing iterators, the comprehension syntax creates iterators and collects them; a pretty common use case.

The point is not to avoid any kind of parallel solution — that neither desirable nor practical — but to have a small number of orthogonal building blocks that combine well. For this particular purpose, control flow already works very well.

On a somewhat related note, I admire how clean Julia is from a control flow perspective. Basic (non-coroutine) control flow has combounding (`begin`, `( ; )`), two kinds of loops (`for`, `while`), branching and short-circuiting boolean ops, and the standard exception handling constructs. And that’s it! We can build the rest easily from these components. (Technically `for` is redundant once we have `while`, but it meshes so well with iteration that it is worth it).

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [July 13, 2021, 7:57pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/7 "2021-07-13T19:57:22Z")

</div>

> [@dstarerstor](#):
>
> ```julia
> for iter if cond
> #stuff here
> end
> 
> ```

This is starting to sound quite a lot like Ruby. I keep finding things like `do A unless B if C` hard to parse.

---

<div class="post-metadata">

**Author:** ![weymouth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weymouth/32/15839_2.png) [@weymouth](https://discourse.julialang.org/u/weymouth)\
**Post date:** [December 2, 2023, 9:06am UTC](https://discourse.julialang.org/t/for-if-syntax/64559/8 "2023-12-02T09:06:52Z")

</div>

I didn’t know about ifs comprehensions. Very handy…

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 2, 2023, 3:23pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/9 "2023-12-02T15:23:12Z")

</div>

Another cute option (not sure about it keeping full performance):

```julia
julia> If(cond) = Base.Fix1(Iterators.filter, cond)
If (generic function with 1 method)

julia> for i in 1:10 |> If(iseven)
       @show i
       end
i = 2
i = 4
i = 6
i = 8
i = 10

```

---

<div class="post-metadata">

**Author:** ![Ken\_Williams](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_williams/32/5289_2.png) [@Ken\_Williams](https://discourse.julialang.org/u/Ken_Williams)\
**Post date:** [June 3, 2024, 4:46pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/10 "2024-06-03T16:46:39Z")

</div>

> [@Tamas\_Papp](#):
>
> Using the `[... for ... in ... if ...]` syntax avoids creating temporaries for the unfiltered interim results, boolean indexing OTOH would require them.

I didn’t quite follow this part - what’s different about `for` loops, compared to comprehensions, that would necessitate creating temporary values for interim results?

FWIW I (a relative Julia newcomer) just found this conversation after expecting the `for ... if ...` syntax to work, since it already works in comprehensions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 3, 2024, 5:43pm UTC](https://discourse.julialang.org/t/for-if-syntax/64559/11 "2024-06-03T17:43:40Z")

</div>

> [@Ken\_Williams](#):
>
> I didn’t quite follow this part - what’s different about `for` loops, compared to comprehensions, that would necessitate creating temporary values for interim results?

It’s the other way around. Comprehensions inherently allocate arrays, `for` loops do not. In the former case (comprehensions), it’s useful to provide an `if` statement as part of the syntax, so that you don’t allocate an array bigger than necessary. In the latter case (`for` loops), there is no need for a special syntax, since you can just put an ordinary `if` statement in the loop body.

Also, the argument to a comprehension is just a generator expression, and generator expressions can be used anywhere an iterator is allowed — including comprehensions, function arguments, and `for` loops. e.g.:

```julia
julia> sum(i for i = 1:10 if i%2 == 0) # == sum([2, 4, 6, 8, 10])
30

julia> for i in (i for i = 1:10 if i%2 == 0)
          @show i
       end
i = 2
i = 4
i = 6
i = 8
i = 10

```
