# Confusion about nested for loop output

**URL:** <https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835>\
**Category:** New to Julia\
**Created:** [June 28, 2026, 7:46pm UTC](https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835 "2026-06-28T19:46:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [June 28, 2026, 7:46pm UTC](https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835/1 "2026-06-28T19:46:05Z")

</div>

I realise this is old and tired. I disagreed with the scope changes of 1.5, but I am not trying to open that discussion. I am just genuinely puzzled by the following in the REPL:

```julia-auto
for outer = 1:2
                 println("outer = $outer before inner loop")
                 for inner = 11:12
                    outer = outer + 7
                    println(inner + outer)
                 end
                 println("outer = $outer after inner loop")
              end
outer = 1 before inner loop
19
27
outer = 15 after inner loop
outer = 2 before inner loop
20
28
outer = 16 after inner loop

```

I know that the line `outer = outer + 7` does not throw a warning because `outer`, while not local to the inner loop, is not a global variable either. What puzzles me is that the assignment in the inner loop first persists, hence the `outer = 2 before inner loop` that appears as a result of `println(outer)` after after the first pass through the inner loop.

What puzzles me is that returning to the top of the code body of the first loop, `outer` is again determined by the sequence defined by the specification of the loop values. I guess effectively there is another assignment, and that a hidden variable keeps track of what the actual value of `outer` should be at the start of each pass through the outer loop, and that this value is assigned to `outer` at that point, undoing whatever may have been assigned to `outer` during the rest of the pass through the outer loop (including of course all passes through the inner loop).

Have I got it right? And if so, where is this immutability of the sequence if loop values documented (apologies again for asking, I seem to remember it _is_ documented but I don’t remember where)?

---

<div class="post-metadata">

**Author:** ![aryavorskiy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aryavorskiy/32/43466_2.png) [@aryavorskiy](https://discourse.julialang.org/u/aryavorskiy)\
**Post date:** [June 28, 2026, 7:53pm UTC](https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835/2 "2026-06-28T19:53:19Z")

</div>

Hi!  
You can think of the outer for loop as something like this:

```julia-auto
itr = iterate(1:2)
while itr !== nothing
    outer, state = itr
    # loop body
    itr = iterate(1:2, state)
end

```

So yeah, there is an assignment overwriting `outer`. This has nothing to do with scoping and everything to do with a stateless iterator

---

<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 28, 2026, 8:36pm UTC](https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835/3 "2026-06-28T20:36:59Z")

</div>

> [@Henri\_Laurie](#):
>
> Have I got it right? And if so, where is this immutability of the sequence if loop values documented (apologies again for asking, I seem to remember it _is_ documented but I don’t remember where)?

As documented in the [“loops” section of the manual](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops), the syntax `for x = collection` (or equivalently `for x in collection`) assigns `x` to the contents of any iterable `collection` one by one.

A range `1:2`, in Julia, is semantically just a type of array, with the same contents as `[1, 2]`.

So, the second iteration of your `for outer = 1:2` loop assigns the `outer` variable to the second element of the `1:2` collection, regardless of how you may have re-assigned `outer` during the previous iteration.

In this sense, it is _not_ the same thing as `for (outer = 1; outer <= 2; ++outer)` in C or C++!

---

<div class="post-metadata">

**Author:** ![Henri\_Laurie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henri_laurie/32/5079_2.png) [@Henri\_Laurie](https://discourse.julialang.org/u/Henri_Laurie)\
**Post date:** [June 29, 2026, 7:51am UTC](https://discourse.julialang.org/t/confusion-about-nested-for-loop-output/137835/4 "2026-06-29T07:51:05Z")

</div>

Thanks, very clear and helpful!
