# Change iteration variable

**URL:** <https://discourse.julialang.org/t/change-iteration-variable/98189>\
**Category:** New to Julia\
**Created:** [May 2, 2023, 9:16am UTC](https://discourse.julialang.org/t/change-iteration-variable/98189 "2023-05-02T09:16:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Annaquest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/annaquest/32/47482_2.png) [@Annaquest](https://discourse.julialang.org/u/Annaquest)\
**Post date:** [May 2, 2023, 9:16am UTC](https://discourse.julialang.org/t/change-iteration-variable/98189/1 "2023-05-02T09:16:20Z")

</div>

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

```julia

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

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [May 2, 2023, 9:28am UTC](https://discourse.julialang.org/t/change-iteration-variable/98189/2 "2023-05-02T09:28:18Z")

</div>

`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:

```julia
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
