# Count in Thunkable produces extra iterations

**URL:** <https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473>\
**Category:** Questions about Thunkable X\
**Created:** [February 17, 2023, 10:16pm UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473 "2023-02-17T22:16:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anandavardhana57zwwl](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anandavardhana57zwwl](https://community.thunkable.com/u/anandavardhana57zwwl)\
**Post date:** [February 17, 2023, 10:16pm UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/1 "2023-02-17T22:16:20Z")

</div>

[https://x.thunkable.com/copy/1502f722235b476eac66a0c01558ea0a](https://x.thunkable.com/copy/1502f722235b476eac66a0c01558ea0a)  
You will see that the count, loops three even when the “to value”(length of the) is 0. You can try with 0 values in the “from” of the count and then it loops twice. Only at the higher values it works logically. In the C world:  
If (i = 0; i = 0; i++) {  
…  
}  
would not loop even once.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [February 18, 2023, 12:54am UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/2 "2023-02-18T00:54:14Z")

</div>

So obviously you’ve provided blocks that are a mistake:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/b/0beaae0f6efd06b29c88063dcb91306072a86bb0.png)

And you’re right, they shouldn’t do anything. But instead they act unpredictably. I suppose there should be better error checking with the [count with] block.

But also… why not just avoid a situation like that? Do you think it’s likely you’d use a variable as a range value that might exceed the valid range for an iterative loop? I haven’t run into that situation but it’s certainly conceivable.

---

<div class="post-metadata">

**Author:** ![manyone](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@manyone](https://community.thunkable.com/u/manyone)\
**Post date:** [February 18, 2023, 1:53am UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/3 "2023-02-18T01:53:26Z")

</div>

when you use a count\_with block where the start\_val is greater than end\_val, it means you’re counting down , so your step\_value should really be (-1) as shown below

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/1/813356e8f3a2f0b88f26c2e4c702b127411f0105.png)

and this is the result as expected  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/0/80ccbce81099386a18af004632cacd6fc1386101.png)

it looks like thunkable assumes you’re counting down when it sees this condition, so it automatically adjusts the iterator to have the correct sign. even if you entered 1, it internally becomes -1.

if you think about it, if you use +1 as your step\_value, and you start with 5, you will never get to 4!

(i don’t know if this is a bug or a feature)

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/a/7ade1164f6b22c9fab74c7a39a8ce25eda574f26.png)

also results  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/3/638290fa9608069a4bf8ca360b30bc7263d63e41.png)

this is why your block set up counts twice.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [February 18, 2023, 3:08am UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/4 "2023-02-18T03:08:39Z")

</div>

> [@manyone](#):
>
> it looks like thunkable assumes you’re counting down when it sees this condition, so it automatically adjusts the iterator to have the correct sign. even if you entered 1, it internally becomes -1.

Yes, that’s the conclusion I reached, too.

---

<div class="post-metadata">

**Author:** ![anandavardhana57zwwl](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anandavardhana57zwwl](https://community.thunkable.com/u/anandavardhana57zwwl)\
**Post date:** [February 18, 2023, 5:39am UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/5 "2023-02-18T05:39:48Z")

</div>

Hi Tatiang, As you said it is conceivable. I saw this bug when was doing this block:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/3/a/3aa7dd644ae8d573b9ccb6f82adedb66b5aa822b.png)  
So normally AddRowCount is a large figure and things were working fine. There was some other bug and AddRowCount went to zero and then I got two rows of data!! I was surprised. That is how ran into this issue. In C it would never proceed so the bug would never surface.

Manyone thanks a lot, you analyzed it rightly. It never occurred to me that it was going negative and hence doing the two. Once you explained it is very clear. So technically if one thinks in C wise it is bug but Thunkable doing the right thing as explained.

> > even if you entered 1, it internally becomes -1.  
> > Thanks to both. I have to be more diligent and avoid such situation.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [May 19, 2023, 5:40am UTC](https://community.thunkable.com/t/count-in-thunkable-produces-extra-iterations/2283473/6 "2023-05-19T05:40:40Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
