# Why \`for/if\` syntax is not allowed in loops

**URL:** https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393
**Category:** Internals & Design
**Tags:** loops, control-flow
**Created:** [September 14, 2024, 7:45am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393 "2024-09-14T07:45:22Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [September 14, 2024, 7:45am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/1 "2024-09-14T07:45:22Z")

</div>

Hi,

I wonder why the combination of `for` and `if` is allowed in comprehension like

```julia
 nbe = sum(count(iseven,rv) for rv ∈ rvv if isodd(length(rv));init=0)

```

but has to be written in two separate control flows statement in loops:

```julia
  #separate for and if in the loop form
    nbe = 0
    for rv ∈ rvv 
        if isodd(length(rv))
            nbe += count(iseven,rv)
        end
    end

```

and can’t be written as

```julia
    nbe = 0
    for rv ∈ rvv if isodd(length(rv))
        nbe += count(iseven,rv)
    end

```

> **MWE**
>
> ```julia
> function test_for_if()
> 
> rvv = [[rand(Int) for s ∈ 1:rand(1:10)] for sn ∈ 1:10]
> @show rvv
> 
> #separate for and if in the loop form
> nbe = 0
> for rv ∈ rvv 
> if isodd(length(rv))
> nbe += count(iseven,rv)
> end
> end
>     
> #the comprehension for allows for for-if syntax
> nbe′ = sum(count(iseven,rv) for rv ∈ rvv if isodd(length(rv));init=0)
> 
> @show nbe,nbe′
> 
> #why the following for-if syntax doesnt' apply for loops ?
> # nbe′′ = 0
> # for rv ∈ rvv if isodd(length(rv))
> # nbe′′ += count(iseven,rv)
> # end
> # @show nbe,nbe′,nbe′′
> return nothing
> end
> 
> ```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [September 14, 2024, 9:25am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/2 "2024-09-14T09:25:07Z")

</div>

I think the main reason is that it introduces a parsing ambiguity that depends on whitespace. Your first example needs two `end`. Your second needs one.

The sum doesn’t have this ambiguity because generators to not use `end`.

You can achieve you intend with the somewhat horrific:

```julia
julia> for a in (a for a in 1:3 if isodd(a))
           @show a
       end
a = 1
a = 3

```

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [September 14, 2024, 10:06am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/3 "2024-09-14T10:06:24Z")

</div>

@LaurentPlagne, I am unsure if your question is why the language designers didn’t accommodate the syntax you are inquiring about or why you experience the issue when using the _not supported_ `for/if` combination.

In scenarios like yours - and when comprehension is not the best pick, I like to do something like the following:

```julia
for x in 1:3
    !isodd(x) && continue
    @show x
end
# x = 1
# x = 3

```

---

<div class="post-metadata">

### Author: ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)
#### Post date: [September 14, 2024, 10:19am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/4 "2024-09-14T10:19:28Z")

</div>

I think @odow already sufficiently answered the question of “why”. But if you’re also looking for working alternatives, you could use `Iterators.filter`:

```julia
using .Iterators

nbe3 = 0
for rv in Iterators.filter(isodd ∘ length, rvv)
     nbe3 += count(iseven, rv)
end

```

In other situations you would have to rely on anonymous functions (e.g. `for rv in Iterators.filter(rv -> isodd(length(rv), rvv)`) though, which to me seems more complicated than a simple `if ... end` or `&&`/`||` `continue`.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [September 14, 2024, 11:27am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/5 "2024-09-14T11:27:52Z")

</div>

thank you all for these detailed answers !

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 15, 2024, 4:59pm UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/6 "2024-09-15T16:59:26Z")

</div>

Linking [identical thread](https://discourse.julialang.org/t/for-if-syntax/64559) for further insights.

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [September 20, 2024, 3:53am UTC](https://discourse.julialang.org/t/why-for-if-syntax-is-not-allowed-in-loops/119393/7 "2024-09-20T03:53:53Z")

</div>

I’ll just point out that

> [@LaurentPlagne](#):
>
> ```julia
> for rv ∈ rvv 
> if isodd(length(rv))
> nbe += count(iseven,rv)
> end
> end
> 
> ```

can be written as

```julia
    for rv ∈ rvv if isodd(length(rv))
        nbe += count(iseven,rv)
    end end

```

😉
