# Understanding while loop UndedVarError and scope

**URL:** https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609
**Category:** General Usage
**Created:** [October 21, 2018, 7:07pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609 "2018-10-21T19:07:41Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 21, 2018, 7:07pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/1 "2018-10-21T19:07:41Z")

</div>

REPL, Julia v1.0.1:

```
julia> a=1; 

julia> while true; print(a); break; end
1

julia> while true; a=2; break; end

julia> while true; a=2; print(a); break; end
2

julia> while true; print(a); a=2; break; end

ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope at .\REPL[4]:1 [inlined]
 [2] top-level scope at .\none:0

```

I got some explanations here [https://github.com/JuliaLang/julia/issues/29313#issuecomment-431618955](https://github.com/JuliaLang/julia/issues/29313#issuecomment-431618955)  
but I’m still very confused.

1. It’s obvious that the wording of the error is wrong ( `a` is defined there).
2. In 1st “while”, “print” can see that “a” is 1; in the 3rd “while”, “print” can see that “a” is 2; while in the 4th “while”, it can see neither!

So I welcome any other explanations.  
Also, I’m curious whether this is confusing just to me, or to other people as well?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [October 22, 2018, 1:46am UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/2 "2018-10-22T01:46:12Z")

</div>

So the rule is that you need to specify `global a` to assign values to globals inside loops  
The first while simply reads the global, the second and third use a local with a defined value, and the last wants to use variable a as a local, but there was no value assigned to it yet

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 22, 2018, 2:26pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/3 "2018-10-22T14:26:28Z")

</div>

> [@y4lu](#):
>
> the last wants to use variable a as a local, but there was no value assigned to it yet

From what I can see, there was: I wrote “a=2”, just like in the previous 2 loops.

---

<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: [October 22, 2018, 2:27pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/4 "2018-10-22T14:27:26Z")

</div>

Yes, but you wrote if _after_ `print(a)`. There are two fairly simple things going on here:

1. Having `a = ...` anywhere in a scope makes `a` local.
2. If you access a variable before it has been assigned, it’s an undef error.

Evaluation order does not matter for 1—it’s purely syntactic. Evaluation order does matter for 2.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 22, 2018, 2:56pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/5 "2018-10-22T14:56:41Z")

</div>

That shines light, thanks.  
I had thought Julia, as a dynamic language, would do everything at run time, in a top-down fashion. But it turns out it determines locality of variables kind of at compile time, but the actual evaluation at run time. (Of course it’s more complicated , as it’s JIT compiled…)  
Which makes it seem partially blind in this case, where it can tell that `a` is made local by `a=...` but not the actual value of `a` so that to tell `print` to print `2`.

Overall, I think this makes the behavior complicated, for most of users, compared to other languages.

---

<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: [October 22, 2018, 3:44pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/6 "2018-10-22T15:44:40Z")

</div>

I’m confused by why you expect `a` to have a value before you’ve assigned to it.

---

<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 22, 2018, 3:49pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/7 "2018-10-22T15:49:03Z")

</div>

```julia
julia> a=1; 

julia> while true; print(a); break; end
1

julia> while true; q=2; break; end

julia> while true; q=2; print(q); break; end
2

julia> while true; print(q); q=2; break; end

```

is an equivalent version of the code in OP.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 22, 2018, 4:20pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/8 "2018-10-22T16:20:13Z")

</div>

I expect_ed_`a` to have a value for 2 reasons:

1. `a=1` present before the `while` loop. `while` loop body introducing by default a local scope is a strange (to say the least) idea to me
2. once Julia can tell `a` is made local, even though `a=2` occurs after `print(a)`, somehow I expected it to also “see” the value on the other side of `=` in `a=2`.

I no longer hold the 2nd opinion (after your clarification of just syntactic determination of local scope.)  
I still hold 1st opinion though, quite strongly. I will probably elaborate in another post, in “Development” section.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 22, 2018, 4:23pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/9 "2018-10-22T16:23:52Z")

</div>

Not at all; my version illustrates the conflict between a local and global variable with same name, and user confusion it creates. Your version is not surprising at all, IMO.

---

<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 22, 2018, 4:36pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/10 "2018-10-22T16:36:19Z")

</div>

The two versions are semantically equivalent. Your thread title was “Understanding while loop UndedVarError and scope”, my example was there to help you do that.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 22, 2018, 4:42pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/11 "2018-10-22T16:42:24Z")

</div>

Thank you. Now I see what you mean: they are semantically equivalent for the computer. With my reply above to you “Not quite…” I meant they are not equivalent from the user perspective/expectation.

---

<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: [October 22, 2018, 6:18pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/12 "2018-10-22T18:18:02Z")

</div>

> [@Vic](#):
>
> I still hold 1st opinion though, quite strongly. I will probably elaborate in another post, in “Development” section.

I think you’re going to find [this](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894) very helpful in your research. There’s no need for duplication of that topic.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 23, 2018, 6:19pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/13 "2018-10-23T18:19:47Z")

</div>

ONE MORE QUESTION HERE

I’ve been told repeatedly that An explicit `global` is needed to change a global variable.  
Then why this works (again Repl, v 1.01) ?

```
julia> v=[0 0];
julia> while true; v[1]=1; break; end
julia> v
1×2 Array{Int32,2}:
1 0

```

---

<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 23, 2018, 6:24pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/14 "2018-10-23T18:24:46Z")

</div>

`global` is needed to rebind a variable which is a completely different thing from mutating a container.

`v[1] = 1` is just pretty syntax for `setindex!(v, 1, 1)`.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 23, 2018, 6:34pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/15 "2018-10-23T18:34:12Z")

</div>

OK.  
But the effect is exactly the same as `v=[1 0]` !

I thought the intent of all these `global`s was to prevent modifying stuff in global scope unintentionally  
From [https://docs.julialang.org/en/v1/manual/variables-and-scoping/:](https://docs.julialang.org/en/v1/manual/variables-and-scoping/:)

> Avoiding changing the value of global variables is considered by many to be a programming best-practice.

This is all… overcomplicated.

---

<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 23, 2018, 6:48pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/16 "2018-10-23T18:48:39Z")

</div>

> [@Vic](#):
>
> But the effect is exactly the same as `v=[1 0]` !

No, it is definitely not.

Case 1, using `v[1] = 1`

```julia
v = [0 0]
x = v
v[1] = 1
x

```

printing `x` now shows `[1 0]`.

Case 2, using `v = [1 0]`

```julia
v = [0 0]
x = v
v = [1 0]
x

```

printing `x` now shows `[0 0]`.

Scoping is about what value (or object) a particular symbol is bound to. Assignment changes that. Mutation does not.

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 23, 2018, 7:11pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/17 "2018-10-23T19:11:47Z")

</div>

I don’t understand how your explanation is relevant to what I was saying (I don’t blame you).

I knew that in Julia assignment can bind several names to the same object, and mutating the object via one name results in all names pointing now to the new object

But in the context I talked about, I used only 1 name, `v`, and was stating that allowing `v[1]=1` inside `while` to mutate global `v` would be the same as if Julia allowing direct `v = [1 0]` inside that loop to modify that same global `v`.

> Scoping is about what value (or object) a particular symbol is bound to. Assignment changes that. _Mutation does not._

With `s =[0 0]` , then does not `s[1]=1` result in now symbol `s` being bound to a new object, `[1 0]`, as compared to `[0 0]`?

(I don’t mean the internal implementation details, but the end result)

---

<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 23, 2018, 7:24pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/18 "2018-10-23T19:24:59Z")

</div>

> [@Vic](#):
>
> With `s =[0 0]` , then does not `s[1]=1` result in now symbol `s` being bound to a new object, `[1 0]` , as compared to `[0 0]` ?

No, it means that the object `s` has been modified but it is the same object.

```julia
julia> s = [0 1];

julia> pointer_from_objref(s)
Ptr{Nothing} @0x000000010e6324d0

julia> s[1] = 1;

julia> pointer_from_objref(s) # the same as before
Ptr{Nothing} @0x000000010e6324d0

julia> s = [1 1];

julia> pointer_from_objref(s) # different, new object
Ptr{Nothing} @0x000000010ecf55d0

```

---

<div class="post-metadata">

### Author: ![Vic](https://avatars.discourse-cdn.com/v4/letter/v/9e8a1a/32.png) [@Vic](https://discourse.julialang.org/u/Vic)
#### Post date: [October 23, 2018, 7:38pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/19 "2018-10-23T19:38:33Z")

</div>

> [@kristoffer.carlsson](#):
>
> No, it means that the object `s` has been modified but it is the same object.

That’s sounds like a logical self-contradiction to me 🧐

You are showing that with mutation, the _address_ of the object remains the same: but then you interpret this to mean that the _object_ stays the same as well.

I “prove” the “same-ness” of the 2 objects like this 🙂

```
s=[0 0]; s[1] = 1;
s == [1 0]
true
s = [1 0];
s == [1 0]
true

```

---

<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 23, 2018, 7:49pm UTC](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609/20 "2018-10-23T19:49:31Z")

</div>

> [@Vic](#):
>
> That’s sounds like a logical self-contradiction to me

It isn’t though. There is a difference between repainting a house and building a new identical house with a new color. As a case where this would matter, burning down the new identical house is likely ok while burning down the repainted house would likely get you in trouble with the people who own it.

> [@Vic](#):
>
> You are showing that with mutation, the _address_ of the object remains the same: but then you interpret this to mean that the _object_ stays the same as well.

Scope has to do with the bindings of symbols to objects, not the content of the objects.

> [@Vic](#):
>
> I “prove” the “same-ness” of the 2 objects like this

Having different definitions than everyone else means that there will be trouble when you communicate with other people. I’ve already shown you where your definition of “same-ness” and two objects being identical matters.

[Next page](https://discourse.julialang.org/t/understanding-while-loop-undedvarerror-and-scope/16609.md?page=2)
