# Repeating a for loop sequence

**URL:** <https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008>\
**Category:** General Usage\
**Tags:** question, loops\
**Created:** [December 8, 2019, 1:42pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008 "2019-12-08T13:42:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 8, 2019, 1:42pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/1 "2019-12-08T13:42:17Z")

</div>

Hey people,

I basically want to include an if condition into my for loop, which repeats the loop with the previews iteration variable, if the condition hasn’t been satisfied.  
In this case there are a few things to consider:  
First, the loop hast to fill all entries of A.  
Since A should be filled with values greater than 4, I wanted to implement a condition wich repeats the loop if S is smaller than 4.

In this example, A[n] equals 0, if the condition wasn’t satisfied for the n’th loop.

A = zeros(Float64,6)  
counter = 1  
for n = counter:length(A)  
S = rand(1:10)

```
if S > 4
    A[n] = S
else
    n = counter -1 (obviously this didn't work)
end

```

end

print(A)

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 8, 2019, 1:51pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/2 "2019-12-08T13:51:22Z")

</div>

```julia
A = zeros(Float64,6)

for n in eachindex(A)
local S
while true
    S = rand(1:10)
    if S > 4; break; end
end
A[n] = S
end

print(A)

```

Should give you what you want. Note that in this concrete example `S = rand(5:10)` could replace the `while` loop.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 8, 2019, 2:11pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/3 "2019-12-08T14:11:00Z")

</div>

Thanks for your answer!  
Could you maybe explain the commands while ‘true’ and ‘local’.  
Why did you make use of them?

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [December 8, 2019, 2:21pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/4 "2019-12-08T14:21:36Z")

</div>

I used `local S` to declare a local variable named `S` in the scope of the for loop otherwise the `while` loop would create its own local variable `S` that is unknown to the outer `for` loop.  
I used `while true` because I cannot write `while S > 4` before a value was bound to `S`.  
I could have also written

```julia
for n in eachindex(A)
S = rand(1:10)
while S > 4
    S = rand(1:10)
end
A[n] = S
end

```

but I don’t like to write `S = rand(1:10)` twice.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 8, 2019, 2:25pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/5 "2019-12-08T14:25:26Z")

</div>

Indeed, thank you for help !

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [December 8, 2019, 9:49pm UTC](https://discourse.julialang.org/t/repeating-a-for-loop-sequence/32008/6 "2019-12-08T21:49:29Z")

</div>

You can still do it in a much shorter way like this:

```julia
A = zeros(6)

for n in eachindex(A)
    while (S = rand(1:10)) < 5 end
    A[n] = S
end

print(A)

```
