# Variable scope in while loops

**URL:** https://discourse.julialang.org/t/variable-scope-in-while-loops/15886
**Category:** New to Julia
**Tags:** question
**Created:** [October 4, 2018, 3:17pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886 "2018-10-04T15:17:12Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Luis\_de\_Sousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_de_sousa/32/5503_2.png) [@Luis\_de\_Sousa](https://discourse.julialang.org/u/Luis_de_Sousa)
#### Post date: [October 4, 2018, 3:17pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886/1 "2018-10-04T15:17:12Z")

</div>

Hi all,

a bit more of Julia quark I can not fathom:

```julia
julia> a = 10
10

julia> a <= 12
true

julia> while a <= 12
       println(a)
       a += 1
       end
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope at ./none:2

```

This leaves me with two questions:

1. why isn’t the variable visible in the loop guard?
2. how can I use a global (or outer scope) variable in a while loop guard?

Thank you.

---

<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: [October 4, 2018, 3:25pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886/2 "2018-10-04T15:25:50Z")

</div>

You need a `global a` in the `while` loop.

See also [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789) and [Global variables not visible within loops in scripts](https://discourse.julialang.org/t/global-variables-not-visible-within-loops-in-scripts/15849) … this behavior is slated to be changed in interactive contexts. In IJulia/Jupyter it is already changed.

---

<div class="post-metadata">

### Author: ![Luis\_de\_Sousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_de_sousa/32/5503_2.png) [@Luis\_de\_Sousa](https://discourse.julialang.org/u/Luis_de_Sousa)
#### Post date: [October 10, 2018, 2:38pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886/3 "2018-10-10T14:38:58Z")

</div>

I still have not figured how to code a `while` loop like this. Here is what I tried:

```julia
julia> global a = 10
10

julia> while a <= 12
               println(a)
               a = a + 1
           end
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope at ./REPL[3]:2 [inlined]
 [2] top-level scope at ./none:0

julia> while global a <= 12
               println(a)
               global a = a + 1
           end
ERROR: syntax: invalid syntax in "global" declaration

```

I get similar errors with `while` loops in a script.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [October 10, 2018, 2:41pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886/4 "2018-10-10T14:41:37Z")

</div>

```julia
julia> global a = 10
10

julia> while a <= 12
           global a
           println(a)
           a = a + 1
       end
10
11
12

julia> global a = 10
10

julia> while a <= 12
           println(a)
           global a = a + 1
       end
10
11
12

```

---

<div class="post-metadata">

### Author: ![Luis\_de\_Sousa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_de_sousa/32/5503_2.png) [@Luis\_de\_Sousa](https://discourse.julialang.org/u/Luis_de_Sousa)
#### Post date: [October 10, 2018, 2:44pm UTC](https://discourse.julialang.org/t/variable-scope-in-while-loops/15886/5 "2018-10-10T14:44:18Z")

</div>

That was fast! Thanks.
