Change iteration variable

Hi, I get “t is not defined” when I do this:


t = 1
while t <= 200
    println(t)
    if t == 1
        println("Hei")
    elseif t == 140
        t = 150
    else
        println("Hei")
    end
    t += 1
end

I can make it work by using global t = 1. However, I really want to avoid using global variables. How can I fix this? Thank you

t is already a global variable if the snippet you posted is not in a function. To make it a local variable, put your code in a function:

function foo()
    t = 1
    while t <= 200
        println(t)
        if t == 1
            println("Hei")
        elseif t == 140
            t = 150
        else
            println("Hei")
        end
        t += 1
    end
end
foo()

And it will work

4 Likes