UndefVarError on loop iterator in while loop

When I try to run this code I copied from a tutorial, I get an error.

t = 0
while t<5
    println(t)
    t+=1 # t = t + 1
end

LoadError: UndefVarError: n not defined
Did I do something wrong while setting up Julia?

1 Like

In short, in global scope, you would need global t += 1 instead since you are modifying a global variable. In a function, your code would work as is.

See https://github.com/JuliaLang/julia/issues/28789 for some discussion about this.

2 Likes

Which one? Might be a good idea to update this for Julia v1.0.

I got the same issue. This code can be found here: JuliaTutorials/04. Loops.ipynb at main · JuliaAcademy/JuliaTutorials · GitHub
andin the youtube tutorial: A Gentle Introduction to Julia - YouTube