# Variable scope in for loop

**URL:** <https://discourse.julialang.org/t/variable-scope-in-for-loop/41228>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 11, 2020, 8:20pm UTC](https://discourse.julialang.org/t/variable-scope-in-for-loop/41228 "2020-06-11T20:20:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_lin/32/14367_2.png) [@Ken\_Lin](https://discourse.julialang.org/u/Ken_Lin)\
**Post date:** [June 11, 2020, 8:20pm UTC](https://discourse.julialang.org/t/variable-scope-in-for-loop/41228/1 "2020-06-11T20:20:41Z")

</div>

Hi,  
Please see the following code:

```julia
for i = 1:2
    a = 7
    for i = 3
        a = 9
    end
    @show a
    @show i
end

```

which has the output:

```julia
a = 9
i = 1
a = 9
i = 2

```

My question is, why is the value of “i” not modified by the inner for loop? I think its value should be modified just like what happened to variable “a”.

I’m using Julia 1.3.1

Thank you!

---

<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:** [June 11, 2020, 8:28pm UTC](https://discourse.julialang.org/t/variable-scope-in-for-loop/41228/2 "2020-06-11T20:28:49Z")

</div>

The relevant docs section is [For Loops and Comprehensions](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1).

What you want is:

```julia
for i = 1:2
    a = 7
    for outer i = 3
        a = 9
    end
    @show a
    @show i
end

```

Which gives:

```julia
a = 9
i = 3
a = 9
i = 3

```

---

<div class="post-metadata">

**Author:** ![Ken\_Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_lin/32/14367_2.png) [@Ken\_Lin](https://discourse.julialang.org/u/Ken_Lin)\
**Post date:** [June 11, 2020, 8:41pm UTC](https://discourse.julialang.org/t/variable-scope-in-for-loop/41228/3 "2020-06-11T20:41:33Z")

</div>

Thank you! Very helpful!
