# Is there a automatic way to get the index of a For Loop

**URL:** https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299
**Category:** Questions about Thunkable X
**Created:** [February 1, 2023, 7:47pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299 "2023-02-01T19:47:59Z")
**Posts on this page:** 12
**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 1, 2023, 7:47pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/1 "2023-02-01T19:47:59Z")

</div>

![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/2/22849beb20cede7631ec6be2b8fcc6b6fe87e584.png)  
The only way I know is like above. Is there a default way where Thunkable tells the index count? I mean like in C++:  
for (int i = 0; i \< 10; i++) {  
// Here I can break in the mid and the index would be value of i  
}

---

<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 1, 2023, 8:49pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/2 "2023-02-01T20:49:40Z")

</div>

Yes, the count with i from \_ to \_ block is exactly for this purpose.

After you add that block to your project, i will be a variable you can drag into your project from the Variables drawer. It will contain the index (count) of the loop.

---

<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 1, 2023, 9:31pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/3 "2023-02-01T21:31:23Z")

</div>

Thanks you Tatiang but I have a question. This app is just to demo my usage. This app works fine and when it breaks out I get the index value as 3 in OutputTxt which is right. My request is how do I fit in the “count with i …” block into my program? If I can, then I will get rid of the Index variable and the manual way I am keeping track of the index. Please note I do need the “for each item …” block and the if (j = &) block.

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/6/66fa2641d84f8eb809db2df501b0dea06ab88b4c.png)

---

<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 1, 2023, 11:49pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/4 "2023-02-01T23:49:41Z")

</div>

1. Shouldn’t OutputTxt’s Text be 4, not 3, since “&” is the fourth element in the list?

If your lists only contain **one** “&” symbol, there’s a much easier way to do all of this that doesn’t require loops… using the “first occurence of” list block (see this: [Lists - Thunkable Docs](https://docs.thunkable.com/lists#find-first-last-occurrence)):

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/3/4311fb48473f59138a1a7d84cdd93a9804ef1773.png)

If your list may contain more than one “&” symbol then you’ll need to use a loop. Just replace the “for each item j” with a count from 1 to length of app variable InputList and then check to see if the list item at #i = “&”. If so, set OutputTxt’s Text to i.

I’m using i as the loop variable but you may end up using a different letter; it doesn’t matter as long as it’s consistent within your blocks.

---

<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 2, 2023, 5:10am UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/5 "2023-02-02T05:10:15Z")

</div>

Hi Tatiang,  
That is brilliant solution, I tried it but could not make it work. Then Googled and Googled and came up with solution but it is wrong. The issue is it is doing 1-10 times 10 = 100. That is the reason my outputs are corrupted. The #i did not work. I spent couple of hours and finally seeking your help again. I have made it more realistic to my goal. Please advice  
ananda  
[https://x.thunkable.com/copy/1e698113fdf4890cf62d263c5d8642a9](https://x.thunkable.com/copy/1e698113fdf4890cf62d263c5d8642a9)

---

<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 2, 2023, 5:44am UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/6 "2023-02-02T05:44:21Z")

</div>

your for-item loop is INSIDE your count-with-i loop so for i=1, you use the for-loop and visit all 10 elements in InputList - for i=2, you do the same thing - so you would have travelled the one list 10 times.

just stick with the count-with-i loop and you will always get the correct index at anytime (it will be at the i-th position - and when you want to know what item you got atht eh i-th position, just use this block  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/2/c/2cb00e4c947f119a5f290b1bb5c629bdfa6edf18.png)

can you describe what you are trying to do? are you using some kind of table look up such that “%” always returns 234 and “@” always return 86, etc? - are those symbols annunciators of some kind? like after “%” you get latitude and longitude?  
you can probably use objects in a clearer way to do something like that.

---

<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 2, 2023, 6:50am UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/7 "2023-02-02T06:50:13Z")

</div>

Thanks a lot ManyOne. I did some work still could not figure it out.  
[https://x.thunkable.com/copy/4963111703c2c3bd8912f85fd01ef4b6](https://x.thunkable.com/copy/4963111703c2c3bd8912f85fd01ef4b6)  
Very sleepy will work on this in the morning. The output I want to see is this:  
FoundTxt: Found % at index 1 Found & at index 4 Found @ at index 6 Found 56 at index 8  
OutputTxt:45 234 467567 467567 56 89  
Please advice, thanks

---

<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 2, 2023, 7:04am UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/8 "2023-02-02T07:04:56Z")

</div>

is your input always like this? (are the symbols always in the same order? I)  
“40”, “%”, “50”, “60”, “&” ,“70”, “@”, “80”, “$”, “90”  
can it be like this? (can there be 0 or n number before each symbol?)  
“40”,“50”, “%”, “66”, “&” ,“77”,“88”,“99”, “@”, “111”, “$”, “129”,“200”  
or like this? (can the symbols be in any order - is it ok t have zero numbers between symbols"?  
“33"”$“,”&“,“44”,“55”,”@“,”%"  
is the string you are analyzing generated by machine?

---

<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 2, 2023, 7:21am UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/9 "2023-02-02T07:21:49Z")

</div>

Please see this modification to your project (the first link you shared) and let me know if that’s what you had in mind:

[https://x.thunkable.com/copy/bd5b5e963aeef877bc520728b8b6b8e0](https://x.thunkable.com/copy/bd5b5e963aeef877bc520728b8b6b8e0)

Why are you wanting the “%” to be found at index 1? Do you realize that Thunkable lists start at index 1 and that the “%” is actually in item #2 of your list? Many programming environments start with list index 0 so that may be part of the confusion here.

---

<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 2, 2023, 2:03pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/10 "2023-02-02T14:03:00Z")

</div>

Wow thanks Tatiang you made my day!! 🙏. ManyOne thanks to you also helping me with the right block to use. Now Good morning to you both, let me clarify both of your queries from earlier replies and the current ones. I am from IBM punch card era 🙂 Later on C++ and System programming(At Processor-OS interaction level) world. All my 40 + years of programming life starting index is 0 or you can say bit 0. So the slight confusion between us. I tend to think all programming issues in C++ way hence get caught in my own web. You people think in thunkable way a very abstracted way that is just brilliant. The data, demarkers and their positions are all fictitious created just to illustrate my problem. They are totally random. Thanks once again for both of you. I will get back to implementing it in my real program!!

---

<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 2, 2023, 2:07pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/11 "2023-02-02T14:07:18Z")

</div>

Hi Manyone,

Thanks for the good tip and it got me almost to the solution then I had to sleep, I have answered your questions in the reply to Taitiang. As I said this is just some random data to illustrate my issues.

regards  
ananda

---

<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 3, 2023, 2:07pm UTC](https://community.thunkable.com/t/is-there-a-automatic-way-to-get-the-index-of-a-for-loop/2253299/12 "2023-05-03T14:07:56Z")

</div>

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