# Scope of variable in if in loop

**URL:** <https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 9, 2021, 12:31pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621 "2021-06-09T12:31:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [June 9, 2021, 12:31pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/1 "2021-06-09T12:31:42Z")

</div>

from my understanding, “if” doesn’t create new scope, so the variable “b” should be local variable in for loop. So why there is an error saying b is not found in the “else” ?

```julia
function f1(a)
  for i = 1:2
    if i ==1
      b = a +1
      println("a = $a, b = $b")
    else
      println("b = $b")
    end
 end
end

```

```julia
f1(2)
a = 2, b = 3
ERROR: UndefVarError: b not defined

```

---

<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:** [June 9, 2021, 12:35pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/2 "2021-06-09T12:35:44Z")

</div>

> [@Fred\_He](#):
>
> So why there is an error saying b is not found in the “else” ?

~~I think because the compiler isn’t able to figure out that the `i==1` branch of the `if` (which defines `b`) always executes first, so at compile-time it thinks that it’s possible that `b` is not defined yet in the `else` branch.~~ See below for the correct answer.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 9, 2021, 12:45pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/3 "2021-06-09T12:45:05Z")

</div>

The behavior you see is the expected behavior. The `if` statement does not create a new scope, but the `for` creates a new scope _for each iteration_. So `b` will never be defined inside any loop besides the first. You need to declare it one scope above for your code to work (i.e., outside the `for`).

I wrote a better answer to a very similar question in the past: [I thought I understood "pass-by-sharing" in Julia until I found this - #19 by Henrique\_Becker](https://discourse.julialang.org/t/i-thought-i-understood-pass-by-sharing-in-julia-until-i-found-this/46897/19)

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [June 9, 2021, 12:52pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/4 "2021-06-09T12:52:35Z")

</div>

Thanks for the answer. I assume if I want to improve performance, it’s better to declare variables used in for loop?

---

<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:** [June 9, 2021, 1:28pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/5 "2021-06-09T13:28:39Z")

</div>

> [@Fred\_He](#):
>
> I assume if I want to improve performance, it’s better to declare variables used in for loop?

No, variable declarations are not generally needed for performance. Just make sure your variable usage is [type-stable](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-changing-the-type-of-a-variable) (and read the other performance tips).

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 9, 2021, 1:56pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/6 "2021-06-09T13:56:42Z")

</div>

Maybe there is a confusion here. If the variable in case ~~is~~ was not a scalar or some other immutable, certainly you should create it outside the loop and mutate it inside to avoid creating a new mutable variable at every iteration.

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [June 9, 2021, 2:08pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/7 "2021-06-09T14:08:03Z")

</div>

Thank you! this is very useful!

---

<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:** [June 9, 2021, 2:33pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/8 "2021-06-09T14:33:18Z")

</div>

> [@lmiq](#):
>
> If the variable in case is not a scalar or some other immutable, certainly you should create it outside the loop and mutate it inside to avoid creating a new mutable variable at every iteration.

To be clear, the variable in the code above is an `Int` (an immutable type)— there is no cost to “creating” new `Int` variables in every iteration vs declaring them outside the loop.

---

<div class="post-metadata">

**Author:** ![Fred\_He](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fred_he/32/14298_2.png) [@Fred\_He](https://discourse.julialang.org/u/Fred_He)\
**Post date:** [June 9, 2021, 3:13pm UTC](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621/9 "2021-06-09T15:13:02Z")

</div>

Yes, when you said immutable or mutable I thought about arrays (which is the variable i actually use), declaring or not really makes difference.
