# Undefined variable in loop

**URL:** <https://discourse.julialang.org/t/undefined-variable-in-loop/72936>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 11, 2021, 8:15am UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936 "2021-12-11T08:15:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![biglari](https://avatars.discourse-cdn.com/v4/letter/b/e9bcb4/32.png) [@biglari](https://discourse.julialang.org/u/biglari)\
**Post date:** [December 11, 2021, 8:15am UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936/1 "2021-12-11T08:15:01Z")

</div>

d = 0  
while ym \<= 1 && d \< dotmax  
d = d+1  
end  
This part of my program gives this error. What is the reason?  
LoadError: UndefVarError: d not defined

---

<div class="post-metadata">

**Author:** ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)\
**Post date:** [December 11, 2021, 2:27pm UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936/3 "2021-12-11T14:27:59Z")

</div>

This is a common scoping issue that I encountered. The loop doesn’t have the variable `d` in its scope, it creates its own local variable `d` which is then currently undefined. This is a unique aspect of working in the global scope and it’s one of the reasons why it is recommended to try to work in more locally defined scopes such as in functions and in `let` blocks:

```julia
function example_that_works()
    d = 0
    while ym <= 1 && d < dotmax
        d = d+1
    end
end
example_that_works()

# This also works because "let" has its own local scope
let
    d = 0
    while ym <= 1 && d < dotmax
        d = d+1
    end
end

```

Otherwise, you can fix this by declaring `d` as global:

```julia
global d = 0

```

but this is not recommended for performance reasons (better to just wrap everything in a `main()` function):

This behaviour can seem unintuitive, and I don’t fully understand it myself, but it’s been discussed before [here](https://github.com/JuliaLang/julia/issues/28789).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 11, 2021, 3:14pm UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936/4 "2021-12-11T15:14:45Z")

</div>

What version of Julia are you using? In recent versions, this would work in the REPL or give you a specific error in a script indicating that you need to explicitly mark the variable in the loop as global. It’s highly recommended to use the most recent stable release, which would be 1.6 currently.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 11, 2021, 3:24pm UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936/5 "2021-12-11T15:24:03Z")

</div>

Most recent stable release is 1.7.0, but 1.6 should have the same behavior.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 11, 2021, 3:57pm UTC](https://discourse.julialang.org/t/undefined-variable-in-loop/72936/6 "2021-12-11T15:57:46Z")

</div>

Hah, I forgot that we managed to make that release finally 😂
