# Simple loop to use previous loop value

**URL:** <https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537>\
**Category:** New to Julia\
**Created:** [April 13, 2020, 11:41pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537 "2020-04-13T23:41:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 13, 2020, 11:41pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/1 "2020-04-13T23:41:17Z")

</div>

I have issue to run this supposedly simple loop. Tested two methods, but both spit errors “ERROR: LoadError: UndefVarError: ii not defined”. My objective to use the “i” from previous loop and current loop.

```
#method 1
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

```

Thanks.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 13, 2020, 11:42pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/2 "2020-04-13T23:42:50Z")

</div>

This can be sometimes unintuitive behavior when working in the REPL. Put `global ii` at the top of your `for` loop. For more info see the [docs](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Local-Scope-1).

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 13, 2020, 11:47pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/3 "2020-04-13T23:47:08Z")

</div>

Did that, but still errors. I ran this in Atom/Juno

```
#Julia ver. 1.4
global ii
#method 1
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    if i > 0
        println(ii)
    end
    ii = i
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 13, 2020, 11:55pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/4 "2020-04-13T23:55:30Z")

</div>

Sorry if I wasn’t clear,

you want this:

```julia
#Julia ver. 1.4
#method 1
for i in range(0.0, length=6, step=0.1)
    global ii
    if i > 0
        println(ii)
    end
    ii = i
end

#method 2
ii = 0.0
for i in range(0.0, length=6, step=0.1)
    global ii
    if i > 0
        println(ii)
    end
    ii = i
end

```

You can also wrap it in a `let` block.

```julia
let ii = 0
    for i in range...
        ...
    end
end

```

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 13, 2020, 11:59pm UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/5 "2020-04-13T23:59:32Z")

</div>

Thank you. It is working.

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 14, 2020, 12:05am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/6 "2020-04-14T00:05:33Z")

</div>

I also found alternative solution from the link below. Adapted for my problem above.  
[https://stackoverflow.com/questions/48345168/julia-previous-and-next-values-inside-a-for-loop](https://stackoverflow.com/questions/48345168/julia-previous-and-next-values-inside-a-for-loop)

```
List1 = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5]

for i = 2:length(List1)
	println("The previous letter of $(List1[i]) is $(List1[i-1])")
end

```

OR

```
x = collect(0.0:0.1:0.5)
@show x
for i in 2:length(x)
    println(x[i])
    println(x[i-1])
    ii = i
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 14, 2020, 12:17am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/7 "2020-04-14T00:17:07Z")

</div>

Check out the package [ShiftedArrays](https://juliaarrays.github.io/ShiftedArrays.jl/latest/) for more easy lags and leads.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [April 14, 2020, 12:43am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/8 "2020-04-14T00:43:05Z")

</div>

You are hurting my head. In the original question you appear to want to calculate a value in one loop and use that value in another. In the stack overflow solution you want to get the previous value in a list.

These are two different actions, and have two different solutions…It might make sense in the future to state what you are trying to do at a high level, rather than asking for some low level solution. You should get an answer that solves the correct problem.

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 14, 2020, 4:49am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/9 "2020-04-14T04:49:42Z")

</div>

Sorry if I caused confusion. My objective has not changed, i.e. to get current value and previous loop value and doing something with the data. The example from [stackoverflow.com](http://stackoverflow.com) is pretty much shows what I desired. My original question can be expanded to have the loop shows both current “i” value and previous “i” value. The list can be made from range() function or anything similar e.g. collect() function.

---

<div class="post-metadata">

**Author:** ![Humphrey\_Lee](https://avatars.discourse-cdn.com/v4/letter/h/a88e4f/32.png) [@Humphrey\_Lee](https://discourse.julialang.org/u/Humphrey_Lee)\
**Post date:** [April 14, 2020, 4:59am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/10 "2020-04-14T04:59:05Z")

</div>

Thanks @pdeffebach for the pointer. Not sure how this can help. It may make my code more complicated. I just want to loop over the range() for data binning, i.e. for lower bound and upper bound.

---

<div class="post-metadata">

**Author:** ![lungben](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lungben/32/12314_2.png) [@lungben](https://discourse.julialang.org/u/lungben)\
**Post date:** [April 14, 2020, 6:52am UTC](https://discourse.julialang.org/t/simple-loop-to-use-previous-loop-value/37537/11 "2020-04-14T06:52:29Z")

</div>

Note that `x` is completely allocated in memory in this solution (in contrast to `range`, which is an iterator), therefore you should not use this approach for a range with many elements.
