# Why FOR Loop last iteration ITEM not available GLOBALLY?

**URL:** <https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046>\
**Category:** General Usage\
**Tags:** loops\
**Created:** [May 23, 2020, 8:26pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046 "2020-05-23T20:26:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![santokalayil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santokalayil/32/15038_2.png) [@santokalayil](https://discourse.julialang.org/u/santokalayil)\
**Post date:** [May 23, 2020, 8:26pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/1 "2020-05-23T20:26:10Z")

</div>

Can Someone help me:

> I am from a python background. for loop last interation item variable is globally available in Python. But Julia throws error. Why is it so?. Is there any other way to make that last iteration item variable globally available ie., outside the FOR loop?

**Please see the code in Julia & Python for reference**  
_Julia Code:_

```julia
for i in 1:8897979797897897
    try
        Char(i)
        continue
    catch
        println("Inside Loop",i)
        break
    end
end
println("Outside Loop",i)

```

_ **Julia Output:** _  
Inside Loop2097152  
UndefVarError: i not defined

Stacktrace:  
[1] top-level scope at In[59]:10

* * *

_ **Python Code:** _

```julia
for i in range(8897979797897897):
    try:
        chr(i)
        continue
    except:
        print("Inside Loop",i)
        break
print('Outside Loop', i)

```

_ **Python Output:** _  
Inside Loop 1114112  
Outside Loop 1114112

* * *

Thanks in Advance:  
Santo K Thomas (Mail: [santokalayil@gmail.com](mailto:santokalayil@gmail.com))

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 23, 2020, 8:45pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/2 "2020-05-23T20:45:19Z")

</div>

When you ask why, do you want to know scoing rules defined in the language, or the rationale for making language design choices? Here’s the rules, if that’s what you want:  
[https://docs.julialang.org/en/v1/manual/variables-and-scoping/](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

**Author:** ![santokalayil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santokalayil/32/15038_2.png) [@santokalayil](https://discourse.julialang.org/u/santokalayil)\
**Post date:** [May 23, 2020, 8:49pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/3 "2020-05-23T20:49:03Z")

</div>

Thanks man… I got it…

```julia
for i in 1:8897979797897897
    try
        Char(i)
        continue
    catch
        println("Inside Loop",i)
        global x = i
        break
    end
end
println("Outside Loop",x)

```

As [stillyslalom](https://discourse.julialang.org/u/stillyslalom) says:

> "In general, it’s best to avoid global scope, and you could instead write a function that returns the error-producing `i` "

**i.e.,**

```julia
function errorchar(limit)
    for i in 1:limit
        try
            Char(i)
        catch 
            return i
        end
    end
end

```

Thank you very much Jeff\_Emmanuel & [stillyslalom](https://discourse.julialang.org/u/stillyslalom)

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [May 23, 2020, 8:52pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/4 "2020-05-23T20:52:40Z")

</div>

> [@santokalayil](#):
>
> Is there any other way to make that last iteration item variable globally available ie., outside the FOR loop?

Sure

```julia
julia> for i in 1:10
           global outeri = i
           i>5 && break
       end

julia> outeri
6

```

As for why - it’s a design decision to make loops have their own scope. It takes some getting used to coming from python, but it ultimately leads to better practice.

Edit: ahh you beat me to it!

---

<div class="post-metadata">

**Author:** ![santokalayil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santokalayil/32/15038_2.png) [@santokalayil](https://discourse.julialang.org/u/santokalayil)\
**Post date:** [May 23, 2020, 8:53pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/5 "2020-05-23T20:53:48Z")

</div>

thanks man… I got it … thanks for taking time for immediate response…

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [May 23, 2020, 8:54pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/6 "2020-05-23T20:54:42Z")

</div>

In general, it’s best to avoid global scope, and you could instead write a function that returns the error-producing `i`:

```julia
function errorchar(limit)
    for i in 1:limit
        try
            Char(i)
        catch 
            return i
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![santokalayil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santokalayil/32/15038_2.png) [@santokalayil](https://discourse.julialang.org/u/santokalayil)\
**Post date:** [May 23, 2020, 8:55pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/7 "2020-05-23T20:55:42Z")

</div>

Is it possible that Object Oriented Programming will eventually come to Julia as well?

---

<div class="post-metadata">

**Author:** ![EHBaozi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ehbaozi/32/14854_2.png) [@EHBaozi](https://discourse.julialang.org/u/EHBaozi)\
**Post date:** [May 23, 2020, 9:05pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/8 "2020-05-23T21:05:33Z")

</div>

Probably not. Multiple dispatch is way more powerful and flexible than OOP.

---

<div class="post-metadata">

**Author:** ![santokalayil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/santokalayil/32/15038_2.png) [@santokalayil](https://discourse.julialang.org/u/santokalayil)\
**Post date:** [May 23, 2020, 9:16pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/9 "2020-05-23T21:16:08Z")

</div>

thanks…

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 23, 2020, 9:23pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/10 "2020-05-23T21:23:31Z")

</div>

Multiple dispatch is a form of object orientation, just not the class-based kind. Which is a [good thing](https://www.youtube.com/watch?v=kc9HwsxE1OY).

---

<div class="post-metadata">

**Author:** ![EHBaozi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ehbaozi/32/14854_2.png) [@EHBaozi](https://discourse.julialang.org/u/EHBaozi)\
**Post date:** [May 23, 2020, 9:31pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/11 "2020-05-23T21:31:50Z")

</div>

Indeed. In fact, if anyone wants to have python’s way of things, they already can replicate that with Julia: simply define a `struct` and have all associated methods accept an instance of it as their first argument. After all in python `instanceOfClass.methodcall(*args)` is just a syntactic sugar of `Class.methodcall(instanceOfClass, *args)`, and it’s easy to do the same in Julia. Class inheritance can always be replaced by composition (which can often be a better option anyway). But of course the real magic of Julia can only be unleashed when we really embrace multiple dispatch.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [May 23, 2020, 10:44pm UTC](https://discourse.julialang.org/t/why-for-loop-last-iteration-item-not-available-globally/40046/12 "2020-05-23T22:44:26Z")

</div>

I don’t think I have ever seen it used in the wild, but there is the `outer` keyword that you could use for this; see [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#For-Loops-and-Comprehensions-1).
