# Error: variable inside a loop

**URL:** <https://discourse.julialang.org/t/error-variable-inside-a-loop/21504>\
**Category:** General Usage\
**Created:** [March 5, 2019, 7:51pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504 "2019-03-05T19:51:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raff](https://avatars.discourse-cdn.com/v4/letter/r/5e9695/32.png) [@raff](https://discourse.julialang.org/u/raff)\
**Post date:** [March 5, 2019, 7:51pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/1 "2019-03-05T19:51:48Z")

</div>

I do not understand why it’s an error:

```julia
x = 0
while x < 5
    x += 1
    println(x)
end

```

I am using Julia 1.1.0 on Windows. This is the error msg:

```julia
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at .\REPL[13]:2 [inlined]
 [2] top-level scope at .\none:0

```

Similar result using for loop:

```julia
x = 0
for i = 1:5
    x += 1
    println(x)
end

```

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 5, 2019, 8:03pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/2 "2019-03-05T20:03:33Z")

</div>

For what i understand (and is not a lot), there is a problem with the scope of the function, as the variables inside the loop can’t access variables outside the loop (for more information, you can read this discourse thread [New Scope Solution](https://discourse.julialang.org/t/new-scope-solution/16707/55) ). One way is to directly use the variable using the `global` prefix:

```julia
x = 0
for i = 1:5
    global x += 1
    println(x)
end

```

But I recommend using directly the iterator:

```julia
for i = 1:5
    println(i)
end

```

If you really need to use that `x`, you can make a function:

```julia
function myfn(x)
for i=1:5    
println(x+i)
end
end

```

so, you have this

```julia
julia> myfn(2)
3
4
5
6
7

```

if you want the x modified, you can do something like this

```julia

function myfn!(x) 
#in julia, the ! means that the function modifies the argument, generally the first one
for i=1:4 # the return statement prints x   
x+=1
println(x)
end
return x
end

```

```julia
julia> x = myfn!(0)
1
2
3
4
5
julia> x
5

```

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [March 5, 2019, 8:21pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/3 "2019-03-05T20:21:13Z")

</div>

For now, I think the easiest thing to do is either (1) stick in Jupyter for these sorts of interactive code, and (2) wrap anything that involves for or while loops in functions (e.g.

```julia
function f()
    x = 0
    while x < 5
        x += 1
        println(x)
    end
end
f()

```

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [March 7, 2019, 3:23am UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/4 "2019-03-07T03:23:53Z")

</div>

It’s worth noting that `let` will also work just as well:

```julia
let x = 0
    while x < 5
        x += 1
    end
    println(x)
end

```

---

<div class="post-metadata">

**Author:** ![epogrebnyak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/epogrebnyak/32/6287_2.png) [@epogrebnyak](https://discourse.julialang.org/u/epogrebnyak)\
**Post date:** [August 24, 2019, 9:55pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/5 "2019-08-24T21:55:46Z")

</div>

Encounterd similar behaviour, this is very unintuitive - own scope in `for` loop. Should be writing in big font in docs about this or through an instructive exception.

---

<div class="post-metadata">

**Author:** ![Narasimman17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/narasimman17/32/14588_2.png) [@Narasimman17](https://discourse.julialang.org/u/Narasimman17)\
**Post date:** [May 5, 2020, 4:55am UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/7 "2020-05-05T04:55:04Z")

</div>

This worked perfectly fine for me.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [May 5, 2020, 4:12pm UTC](https://discourse.julialang.org/t/error-variable-inside-a-loop/21504/8 "2020-05-05T16:12:08Z")

</div>

> One rather important distinction between the previous while loop form and the for loop form is the scope during which the variable is visible. If the variable i has not been introduced in another scope, in the for loop form, it is visible only inside of the for loop, and not outside/afterwards.

From the [Repeated Evaluation: Loops](https://docs.julialang.org/en/v1/manual/control-flow/#man-loops-1) section of the manual. There is also a nice table at the start of the next section [Scope of Variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-scope-table-1) that includes the loop constructs.

> [@epogrebnyak](#):
>
> this is very unintuitive - own scope in `for` loop

I think I have not understood what you said, because every language I know creates a new scope inside a `for` construct.
