# Why isn't \`i\` defined inside this while?

**URL:** <https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [September 1, 2020, 9:31am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882 "2020-09-01T09:31:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Somayeh\_Esmaeili](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/somayeh_esmaeili/32/10955_2.png) [@Somayeh\_Esmaeili](https://discourse.julialang.org/u/Somayeh_Esmaeili)\
**Post date:** [September 1, 2020, 9:31am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882/1 "2020-09-01T09:31:01Z")

</div>

would you please help me why `i` cant be known inside this while as following in julia v1+?

```julia
W=([1.0825998948767477 9.448347129060197 972.5335271289529; 165.861582812015 4.563134610834678 1.0293311999771995], 0)
i=1;
k=size(W[1],2);
while i < k
    if ((abs(round(W[1][1,i],digits=6)-round(W[1][1,i+1],digits=6))>2) && (abs(round(W[1][2,i],digits=6)-round(W[1][2,i+1],digits=6))>2))
        p=[(W[1][:,i],W[1][:,i+1],"T",pq[4])];
        PQ=[PQ;p];
    end
    if ~(i==1)
        if ((W[1][:,i],1) != (W[1][:,i+1],1))
            list=[(W[1][:,i],0)];
        end
        List=[List;list];
    end
    i=i+1;
end

```

this error happened

```julia
UndefVarError: i not defined
top-level scope at untitled-867f699d493f5b9bf0774f2429eafd0e:5
include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

```

Thanks indeed.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 1, 2020, 9:57am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882/2 "2020-09-01T09:57:42Z")

</div>

[Loops introduce a new local scope.](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

Use functions to encapsulate code and to allow for more optimizations and faster code.

---

<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:** [September 1, 2020, 10:04am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882/3 "2020-09-01T10:04:57Z")

</div>

In Julia 1.5 this will work in the REPL and print

```julia
┌ Warning: Assignment to `i` in soft scope is ambiguous because a global variable by
 the same name exists: `i` will be treated as a new local. Disambiguate by using `local i`
 to suppress this warning or `global i` to assign to the existing global variable.

```

when running it as a file.

---

<div class="post-metadata">

**Author:** ![Somayeh\_Esmaeili](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/somayeh_esmaeili/32/10955_2.png) [@Somayeh\_Esmaeili](https://discourse.julialang.org/u/Somayeh_Esmaeili)\
**Post date:** [September 1, 2020, 10:05am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882/4 "2020-09-01T10:05:00Z")

</div>

Thanks very much 🙂

---

<div class="post-metadata">

**Author:** ![Somayeh\_Esmaeili](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/somayeh_esmaeili/32/10955_2.png) [@Somayeh\_Esmaeili](https://discourse.julialang.org/u/Somayeh_Esmaeili)\
**Post date:** [September 1, 2020, 10:08am UTC](https://discourse.julialang.org/t/why-isnt-i-defined-inside-this-while/45882/5 "2020-09-01T10:08:14Z")

</div>

Thank you 🌸. yes it is. I’m converting it to a function.
