# Undefined variable error

**URL:** <https://discourse.julialang.org/t/undefined-variable-error/65685>\
**Category:** New to Julia\
**Created:** [August 2, 2021, 2:27pm UTC](https://discourse.julialang.org/t/undefined-variable-error/65685 "2021-08-02T14:27:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wmen](https://avatars.discourse-cdn.com/v4/letter/w/5f8ce5/32.png) [@wmen](https://discourse.julialang.org/u/wmen)\
**Post date:** [August 2, 2021, 2:27pm UTC](https://discourse.julialang.org/t/undefined-variable-error/65685/1 "2021-08-02T14:27:35Z")

</div>

I tried to run the while loop example (from Visual Studio Code on Windows 10):

n = 0  
while n \< 10  
n += 1  
println(n)  
end

and got the following error:

Warning: Assignment to `n` in soft scope is ambiguous because a global variable by the same name exists: `n` will be treated as a new local. Disambiguate by using `local n` to suppress this warning or `global n` to assign to the existing global variable.  
└ @ c:\wmen\Programming\JuliaProjects\FirstSteps\course2.jl:4  
ERROR: LoadError: UndefVarError: n not defined  
Stacktrace:

the examples with dictionaries, tuples, arrays worked fine. Any ideas? Cheers Markus

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 2, 2021, 2:31pm UTC](https://discourse.julialang.org/t/undefined-variable-error/65685/2 "2021-08-02T14:31:52Z")

</div>

Did you read the text in the warning message? It tells you the solution.

For more information, [this post](https://m3g.github.io/JuliaNotes.jl/stable/loopscopes/) may help.

---

<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:** [August 2, 2021, 4:32pm UTC](https://discourse.julialang.org/t/undefined-variable-error/65685/3 "2021-08-02T16:32:19Z")

</div>

If just starting out, I suggest working in jupyter notebooks, where you will find the scoping rules very intuitive.

---

<div class="post-metadata">

**Author:** ![wmen](https://avatars.discourse-cdn.com/v4/letter/w/5f8ce5/32.png) [@wmen](https://discourse.julialang.org/u/wmen)\
**Post date:** [August 3, 2021, 8:47am UTC](https://discourse.julialang.org/t/undefined-variable-error/65685/4 "2021-08-03T08:47:43Z")

</div>

thanks for the input. It seems you have to put a “global n” declaration in a while loop in order to make it work. A bit strange.

---

<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:** [August 4, 2021, 2:06am UTC](https://discourse.julialang.org/t/undefined-variable-error/65685/5 "2021-08-04T02:06:34Z")

</div>

If you ever have to use global, almost certainly you should change the code. Only weird code would ever require it.

Use jupyter to tinker with scripts, where the scoping is completely intuitive, and then put any code with those sorts of loops inside of functions before running in `.jl` files. You will never see this issue again.
