# While loop, variable not defined

**URL:** https://discourse.julialang.org/t/while-loop-variable-not-defined/82278
**Category:** New to Julia
**Created:** [June 5, 2022, 5:55pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278 "2022-06-05T17:55:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nicoo-c](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicoo-c/32/36941_2.png) [@nicoo-c](https://discourse.julialang.org/u/nicoo-c)
#### Post date: [June 5, 2022, 5:55pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278/1 "2022-06-05T17:55:29Z")

</div>

Hi, I’m doing the Introduction to Julia course ([https://juliaacademy.com/courses/375479/lectures/5816184](https://juliaacademy.com/courses/375479/lectures/5816184)) and this happens when i run the code for a while loop.

 ![while loop](https://global.discourse-cdn.com/julialang/original/3X/6/1/61a1b04939473b7a774b2e15e943c271d37e79e7.png)

¿Could someone explain this to me and tell me the correct way to do it?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [June 5, 2022, 5:58pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278/2 "2022-06-05T17:58:38Z")

</div>

the warning message before the error literally tells you what’s going on, please read it.

In this case, you want to take the `global i` advise probably

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [June 5, 2022, 6:17pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278/3 "2022-06-05T18:17:44Z")

</div>

Welcome @nicoo-c  
You find all you need about this here: [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables)

---

<div class="post-metadata">

### Author: ![nicoo-c](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicoo-c/32/36941_2.png) [@nicoo-c](https://discourse.julialang.org/u/nicoo-c)
#### Post date: [June 5, 2022, 7:16pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278/4 "2022-06-05T19:16:17Z")

</div>

Thanks, I understand now.

---

<div class="post-metadata">

### Author: ![nicoo-c](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicoo-c/32/36941_2.png) [@nicoo-c](https://discourse.julialang.org/u/nicoo-c)
#### Post date: [June 5, 2022, 7:19pm UTC](https://discourse.julialang.org/t/while-loop-variable-not-defined/82278/5 "2022-06-05T19:19:02Z")

</div>

Solved with:

```julia
i = 0

while i < 10
  global i += 1
  println(i)
end

```
