# Resetting Counter in a While loop

**URL:** <https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043>\
**Category:** General Usage\
**Created:** [November 30, 2022, 12:05pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043 "2022-11-30T12:05:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [November 30, 2022, 12:05pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/1 "2022-11-30T12:05:44Z")

</div>

Suppose I want a function to continuously iterate through a set until it is directed to stop. Currently I am using a while loop with an if else statement to reset the counter. So it looks something like.

```julia
a=["a","b"]
function test(a)
    cont = Ref(true)
    number = 1
    @async while cont[]
        if number < 3
            println(a[number])
            number+=1
            sleep(5)
        else
            number = 1 
        end
    end
    return(cont)
end

```

Just wondering if this was the correct approach or if there was a more direct way to do this in Julia?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [November 30, 2022, 12:32pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/2 "2022-11-30T12:32:14Z")

</div>

You could use [`Iterators.cycle`](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.cycle) to loop through your array.

```julia
cont = Ref(true)
for x in Iterators.cycle(a)
    cont[] || break # if cont[] is false, then break
    f(x) # do something with x
end

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [November 30, 2022, 12:51pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/3 "2022-11-30T12:51:54Z")

</div>

awesome thanks! Sorry I’m a little confused about how to break the loop in the REPL. With `@async` if

```julia
j = test(a)

```

then

```julia
j[]=false

```

breaks the loop but that doesn’t seem to work here.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [November 30, 2022, 1:04pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/4 "2022-11-30T13:04:22Z")

</div>

It seems to work for me.

```julia
julia> a = ["a", "b"]
2-element Vector{String}:
 "a"
 "b"

julia> function test2(a)
           cont = Ref(true)
           @async for x in Iterators.cycle(a)
               cont[] || break
               println(x)
               sleep(5)
           end
           return cont
       end
test2 (generic function with 1 method)

julia> c = test2(a);
a

julia> b
a
b
a
julia>

julia> c[]=false
false
# no more output

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [November 30, 2022, 2:04pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/5 "2022-11-30T14:04:35Z")

</div>

oh gotcha sorry I thought your post meant that you didn’t need the `@async` with `Iterators.cycle`. Thanks for clearing that up. Also I could put this in another post if that would be better but was wondering with the `Iterators.cycle` is it still possible to introduce a pause on every ith iteration? So in a while loop and a counter I could do something like

```julia
count = 1 
 while true
    f (a[count:count+n]) # run some function on the first n+1 elements of a
    count+=n
    sleep(5)
end   

```

could I do something like that with `Iterators.cycle` or would a counter still be required?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [November 30, 2022, 2:44pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/6 "2022-11-30T14:44:46Z")

</div>

If you want to pause after every _i_-th iteration, you can wrap the cycle in `enumerate`.

```julia
for (i, x) in enumerate(Iterators.cycle(a))
    cont[] || break
    println(x)
    # Sleep after every fifth iteration
    (i % 5 == 0) && sleep(5)
 end

```

prints

```julia
1 a
2 b
3 c
4 a
5 b
6 c
7 a
[...]

```

If you want to pause after processing every _i_-th element of your input array `a`, then you can just swap `enumerate` and `Iterators.cycle` (i.e., `Iterators.cycle(enumerate(a))`) which will produce pairs `(i, a[i])` forever.

I also see that you sketched something that looks like batch processing (`a[count:count+n]`). If that’s your intention, you can use `Iterators.partition` for that.

For example:

```julia
julia> a = ["a", "b", "c", "d", "e", "f", "g"];

julia> function test(a)
           cont = Ref(true)
           # Use batches of maximum size 3
           batches = Iterators.partition(A, 3)

           @async for (i, x) in enumerate(Iterators.cycle(batches))
               cont[] || break
               println(i, ' ', x)

               if i % 2 == 0
                   println("Sleeping...")
                   sleep(2)
               end
           end
           return cont
       end
test (generic function with 1 method)

julia> c = test(a);

```

prints

```julia
1 ["a", "b", "c"]
2 ["d", "e", "f"]
Sleeping...
3 ["g"]
4 ["a", "b", "c"]
Sleeping...
5 ["d", "e", "f"]
6 ["g"]
Sleeping...
7 ["a", "b", "c"]
8 ["d", "e", "f"]
Sleeping...
9 ["g"]
10 ["a", "b", "c"]
Sleeping...
11 ["d", "e", "f"]
12 ["g"]
Sleeping...
13 ["a", "b", "c"]
14 ["d", "e", "f"]
Sleeping...
[...]

```

It processes the array in batches of size 3, and after processing every second batch, it sleeps.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [December 1, 2022, 12:10pm UTC](https://discourse.julialang.org/t/resetting-counter-in-a-while-loop/91043/7 "2022-12-01T12:10:04Z")

</div>

Sorry just saw this but… wow thank you this is so incredibly helpful!!
