"UndefVarError" in loop

Hello,

I’ve been using Julia 0.6 before and now I’m starting a new project using the 1.0.3 version.

I don’t understand this behavior which I think was different in previous versions.

julia> a = 5
5

julia> for i in 1:5
           a = a + 1
       end
ERROR: UndefVarError: a not defined
Stacktrace:
 [1] top-level scope at ./REPL[19]:2 [inlined]
 [2] top-level scope at ./none:0

Thanks,

Maxime

You have to use global a inside of for.

julia> a = 5
5

julia> for i in 1:5
       global a = a + i
       end

julia> a
20

See: Another possible solution to the global scope debacle

2 Likes

Thanks! That fix the problem.