UndefVarError on loop iterator in while loop


#1

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?


#2

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.


#3

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


#4

I got the same issue. This code can be found here: https://github.com/JuliaComputing/JuliaBoxTutorials/blob/master/introductory-tutorials/intro-to-julia/04.%20Loops.ipynb
andin the youtube tutorial: https://www.youtube.com/watch?v=4igzy3bGVkQ