# Nested for loops, UndefVarError

**URL:** https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285
**Category:** New to Julia
**Created:** [September 21, 2018, 3:13pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285 "2018-09-21T15:13:09Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![akazachk](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@akazachk](https://discourse.julialang.org/u/akazachk)
#### Post date: [September 21, 2018, 3:13pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/1 "2018-09-21T15:13:09Z")

</div>

Hi everyone, I have a pretty basic question. The following code is giving me an UndefVarError, and I can’t understand why.

```julia
n=1
for i in 1:3
    for j in 4:5
        n += 1
    end
end
print(n)

```

The line incrementing `n` is throwing an `UndefVarError: n not defined`.

I know I can avoid the problem by enclosing the code in a `let`, but that seems to be the wrong fix, so I am wondering what I am missing here (probably about scope).

Thanks!

---

<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: [September 21, 2018, 3:14pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/2 "2018-09-21T15:14:06Z")

</div>

The variable `n` is a global variable and you thus need to use `global n += 1` to modify it from local scope (inside the for loop).

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [September 21, 2018, 3:19pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/3 "2018-09-21T15:19:48Z")

</div>

And that’s why enclosing it in a `let` works — it does the opposite thing and makes `n` a local variable (local to the `let`), and local variables can be modified from other nested local scopes.

---

<div class="post-metadata">

### Author: ![akazachk](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@akazachk](https://discourse.julialang.org/u/akazachk)
#### Post date: [September 21, 2018, 3:25pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/4 "2018-09-21T15:25:21Z")

</div>

I see, thanks. Right, in fact, there was no need for the example to have nested for loops.

It seems to be a somewhat counterintuitive feature of Julia. I would have guessed that the local scope of the for loop would inherit the global variables.

Also, running this code on JuliaBox, this issue does not arise for some reason, which made it hard to figure out what is happening. Why doesn’t the error get thrown there?

---

<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: [September 21, 2018, 3:25pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/5 "2018-09-21T15:25:40Z")

</div>

See also [https://github.com/JuliaLang/julia/issues/28789](https://github.com/JuliaLang/julia/issues/28789)

… in IJulia/Jupyter/JupyterLab, and soon in the REPL and other interactive contexts, the plan is for Julia to (re-)enable “soft” scoping rules so that you don’t need such `global` declarations for interactive exploration.

Once you move to non-interactive contexts, you should start putting code into functions and stop modifying global variables, in which case the need for `global` keywords becomes less relevant.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [September 21, 2018, 3:36pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/6 "2018-09-21T15:36:06Z")

</div>

> [@akazachk](#):
>
> I would have guessed that the local scope of the for loop would inherit the global variables.

It does indeed inherit the globals, but the globals are allowed to be shadowed by local variables that get introduced in the scope by an assignment.

---

<div class="post-metadata">

### Author: ![akazachk](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@akazachk](https://discourse.julialang.org/u/akazachk)
#### Post date: [September 21, 2018, 3:54pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/7 "2018-09-21T15:54:59Z")

</div>

Thanks again. I read the thread on GitHub, and I would (respectfully) agree that this behavior is definitely not what first-time users expect (who are used to coding in Python and C++). I’m too new of a user and too late to the table to add anything new or useful to the discussion for v1, but I hope that for v2, this will be resolved.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [September 21, 2018, 3:58pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/8 "2018-09-21T15:58:40Z")

</div>

Yet another one of these scoping posts. I’m glad that we are going to be reverting back to a “soft” scope. Does anyone know a timeline for that (is it salted for v2.0)?

---

<div class="post-metadata">

### Author: ![xaver](https://avatars.discourse-cdn.com/v4/letter/x/b4bc9f/32.png) [@xaver](https://discourse.julialang.org/u/xaver)
#### Post date: [September 23, 2018, 5:45pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/9 "2018-09-23T17:45:48Z")

</div>

I decided to use module block and I am happy with it.

---

<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: [September 23, 2018, 8:44pm UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/10 "2018-09-23T20:44:44Z")

</div>

> [@affans](#):
>
> I’m glad that we are going to be reverting back to a “soft” scope. Does anyone know a timeline for that (is it salted for v2.0)?

The plan is to use soft scope for _interactive_ environments only. IJulia (Jupyter, JupyterLab) _already_ does this, and the REPL will be changed in Julia 1.1.

---

<div class="post-metadata">

### Author: ![peters](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peters/32/5924_2.png) [@peters](https://discourse.julialang.org/u/peters)
#### Post date: [November 3, 2018, 2:28am UTC](https://discourse.julialang.org/t/nested-for-loops-undefvarerror/15285/11 "2018-11-03T02:28:06Z")

</div>

I have recently bumped this counterintuitive (as a Python programmer) behaviour in REPL. I can bet many Julia beginners will raise the same question I had. Appreciate if it is changed when Julia 1.1 rolls out.
