# 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:** 1\
**Showing post:** 2

<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.

---

_[View the full topic](https://discourse.julialang.org/t/scope-of-variable-in-if-in-loop/62621)._
