# While loop doesn't inherit variable from global scope in 1.0

**URL:** <https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088>\
**Category:** New to Julia\
**Created:** [August 26, 2018, 5:38pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088 "2018-08-26T17:38:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajlangley](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@ajlangley](https://discourse.julialang.org/u/ajlangley)\
**Post date:** [August 26, 2018, 5:38pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/1 "2018-08-26T17:38:40Z")

</div>

I copied the below code directly from the docs in Collections and Data Structures: Iteration.

```julia
next = iterate(iter)
while next !== nothing
    (i, state) = next
    # body
    next = iterate(iter, state)
end

```

I defined `iter` as `iter = 1:3`

I’ve tried running it in its own file and in the REPL, and in both cases I get the following error:

`ERROR: LoadError: UndefVarError: next not defined`

I also tried using the global keyword to explicitly define `next` as a global variable, which didn’t resolve the issue. Is this a bug, or am I missing something?

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [August 26, 2018, 6:01pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/2 "2018-08-26T18:01:51Z")

</div>

```julia
julia> let
           next = iterate(iter)
           while next !== nothing
               (i, state) = next
               # body
               next = iterate(iter, state)
           end
       end

julia> 

```

Above works for me.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [August 26, 2018, 6:04pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/3 "2018-08-26T18:04:19Z")

</div>

```julia
julia> iter = 1:3
1:3

julia> while next !== nothing
           global next
           (i, state) = next
           # body
           next = iterate(iter, state)
       end

julia> 

```

Above also works.

---

<div class="post-metadata">

**Author:** ![ajlangley](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@ajlangley](https://discourse.julialang.org/u/ajlangley)\
**Post date:** [August 26, 2018, 6:20pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/4 "2018-08-26T18:20:16Z")

</div>

This works for me as well. Do you have any idea why it’s the case that you are able to test the value of `next` before defining it, or why the code from the docs does not work?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 26, 2018, 6:25pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/5 "2018-08-26T18:25:21Z")

</div>

[Loops](https://docs.julialang.org/en/stable/manual/variables-and-scoping/) introduce a new scope.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [August 26, 2018, 6:29pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/6 "2018-08-26T18:29:16Z")

</div>

Here is more discussion: [Scope and global in REPL (julia 0.7)](https://discourse.julialang.org/t/scope-and-global-in-repl-julia-0-7/10233)

---

<div class="post-metadata">

**Author:** ![ajlangley](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@ajlangley](https://discourse.julialang.org/u/ajlangley)\
**Post date:** [August 26, 2018, 6:32pm UTC](https://discourse.julialang.org/t/while-loop-doesnt-inherit-variable-from-global-scope-in-1-0/14088/7 "2018-08-26T18:32:57Z")

</div>

Thank you. That clears up my confusion.
