# Nested for loop

**URL:** <https://discourse.julialang.org/t/nested-for-loop/94394>\
**Category:** New to Julia\
**Created:** [February 10, 2023, 8:10am UTC](https://discourse.julialang.org/t/nested-for-loop/94394 "2023-02-10T08:10:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![j\_tanu](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@j\_tanu](https://discourse.julialang.org/u/j_tanu)\
**Post date:** [February 10, 2023, 8:10am UTC](https://discourse.julialang.org/t/nested-for-loop/94394/1 "2023-02-10T08:10:59Z")

</div>

HI,  
I am trying to write a nested for loop, and I am not getting the desired output. Possibly I am making some mistakes, and there must be other ways to solve this problem. I am writing down the part of my code.

```julia
for i=1:5
    for j=i+1:5
        if (i+j)%4!=0
           println("i=",i,",j=",j)
        else
            i=j
            break
        end
    end
end

```

I am getting the output as

```julia
i=1,j=2
i=2,j=3
i=2,j=4
i=2,j=5
i=3,j=4
i=4,j=5

```

Instead, I was expecting the following output.

```julia
i=1,j=2
i=3,j=4

```

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [February 10, 2023, 8:18am UTC](https://discourse.julialang.org/t/nested-for-loop/94394/2 "2023-02-10T08:18:53Z")

</div>

To me the code does what I would expect it to do (also Julia novice). Printing out where are i+j is not divisible by 4 (and j\>i). What is the “rule” that you intended?

I believe a more idiomatic and compact way to write this would be

```julia
[(;i=i, j=j) for i in 1:5, j in 1:5 if j>i & (i+j)%4!=0]

```

---

<div class="post-metadata">

**Author:** ![halejulia](https://avatars.discourse-cdn.com/v4/letter/h/fbc32d/32.png) [@halejulia](https://discourse.julialang.org/u/halejulia)\
**Post date:** [February 10, 2023, 8:25am UTC](https://discourse.julialang.org/t/nested-for-loop/94394/3 "2023-02-10T08:25:16Z")

</div>

I think the problem is that you are trying to assign values to `i`

See this part from the Julia documentation

> Multiple nested `for` loops can be combined into a single outer loop, forming the cartesian product of its iterables:

```julia
julia> for i = 1:2, j = 3:4
           println((i, j))
       end
(1, 3)
(1, 4)
(2, 3)
(2, 4)

```

> With this syntax, iterables may still refer to outer loop variables; e.g. `for i = 1:n, j = 1:i` is valid. However a `break` statement inside such a loop exits the entire nest of loops, not just the inner one. **Both variables (`i` and `j`) are set to their current iteration values each time the inner loop runs. Therefore, assignments to `i` will not be visible to subsequent iterations:**

---

<div class="post-metadata">

**Author:** ![mreichMPI-BGC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mreichmpi-bgc/32/43775_2.png) [@mreichMPI-BGC](https://discourse.julialang.org/u/mreichMPI-BGC)\
**Post date:** [February 10, 2023, 8:58am UTC](https://discourse.julialang.org/t/nested-for-loop/94394/4 "2023-02-10T08:58:42Z")

</div>

Ok, yes I misunderstood the intention!
