# A very simple question about for loop in julia

**URL:** https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978
**Category:** New to Julia
**Tags:** question
**Created:** [April 4, 2022, 4:01am UTC](https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978 "2022-04-04T04:01:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tjk](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tjk](https://discourse.julialang.org/u/tjk)
#### Post date: [April 4, 2022, 4:01am UTC](https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978/1 "2022-04-04T04:01:46Z")

</div>

I have the following very simple codes (Julia 1.5.0):

#test\_for.jl

```julia
a = 100
for i=1:10
a = a+1
end
b = a
println("a=",a," b=",b) # I want to get a =110 and b=110

```

When I executed the script, julia says variable ‘a’ is not defined. I searched on the Internet and quite stunned to find out that the variables inside “for” are local variables. So, can anyone provide any solutions that can realize the above code in Julia? Thanks!

The key point is that I cannot understand why the variables inside “for” are defined as “local”, since I didn’t use a function in this case. (LOL…)

I also found out some similar topics, but the focus are different:

> [@How to correctly define and use global variables in the module in Julia?](https://discourse.julialang.org/t/how-to-correctly-define-and-use-global-variables-in-the-module-in-julia/65720):
>
> In fortran, we know that we can in a module define a global variable (use the private property), so that we can use the subroutines in the module to set or change the values of that variable. That updated variable can be used by other functions or subroutines in the module too. See below, module Mod integer, parameter :: r8=selected\_real\_kind(15,9) real(kind=r8), private, save :: var contains subroutine f(x) real(kind=r8) :: x var = 2.0\_r8\*x end subroutine f end As we can see, we can call f(x…

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 4, 2022, 4:16am UTC](https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978/2 "2022-04-04T04:16:48Z")

</div>

Since you’re running it in a script, and not immediately in the repl, it should behave in the `>=1.5` way, assigning a local variable and giving a warning.

[https://docs.julialang.org/en/v1/manual/variables-and-scoping/#On-Soft-Scope](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#On-Soft-Scope)

As for solution, make it `global a = a+1`

---

<div class="post-metadata">

### Author: ![tjk](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@tjk](https://discourse.julialang.org/u/tjk)
#### Post date: [April 4, 2022, 4:19am UTC](https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978/3 "2022-04-04T04:19:44Z")

</div>

So in

```julia
global a = a+1

```

Julia knows ‘a’ at the right of the expression is a global variable, right? This is my understanding, the only problem is that when we use the same variable at the left of the expression inside ‘for’ loop, the conflicts appear.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 4, 2022, 4:21am UTC](https://discourse.julialang.org/t/a-very-simple-question-about-for-loop-in-julia/78978/4 "2022-04-04T04:21:18Z")

</div>

Yes. Since there is no other `a` to be found.
