# Question about UndefVarError in for loop

**URL:** https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442
**Category:** New to Julia
**Created:** [June 19, 2019, 12:00pm UTC](https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442 "2019-06-19T12:00:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RaquelSantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raquelsantos/32/3668_2.png) [@RaquelSantos](https://discourse.julialang.org/u/RaquelSantos)
#### Post date: [June 19, 2019, 12:00pm UTC](https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442/1 "2019-06-19T12:00:42Z")

</div>

@variable(model,Y[1:n], lower\_bound=0)  
T = Float64.(model.TP)  
D = Float64.(model.TD)

Yy = transpose(Y)  
a = Int64  
b = Int64  
a = 1  
b = n

for j in 1:m  
@constraint(model,Yy[1:n].\*T[a:b] .\<= D[j])  
a = a+n  
b = b+n  
end - UndefVarError: a not defined

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 19, 2019, 1:18pm UTC](https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442/2 "2019-06-19T13:18:05Z")

</div>

Please quote your code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

You need `global a = a + n` if you want to modify the global variable `a` from a for loop.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [June 19, 2019, 6:03pm UTC](https://discourse.julialang.org/t/question-about-undefvarerror-in-for-loop/25442/3 "2019-06-19T18:03:20Z")

</div>

Also just as an aside

```julia
a = Int64
b = Int64
a = 1
b = n

```

this doesn’t seem to make sense - the first two lines just make `a` and `b` aliases of `Int64`, i.e. after doing this you can do:

```julia
julia> a = Int64
Int64

julia> a(2.0)
2

```

You might be thinking of languages in which variables have to be declared with type annotations, this is not necessary in Julia. Consider:

```julia
julia> a = 1
1

julia> typeof(a)
Int64

```

although there might be instances where you want to use the type declaration syntax `a::Int64`, see the section on [type declarations](https://docs.julialang.org/en/v1/manual/types/index.html#Type-Declarations-1) in the manual.
