i’m afraid you cannot use the regular programming techniques that you’re used to “Like using for or while loops” in a lot of the cases when working inside commcare. but, don’t be discouraged. there’s usually a way to achieve what you’re after.
so, first things first: this link talks about repeats and how to use them, reference elements inside them, and to use logic with things like “position()”. it’s a great place to start.
as far as the showing of additional repeats (entire repeats, and not just part of it), the app will usually ask you if you wish to add another repeat when you’re done with answering the questions inside of it. But when it comes to removing a repeat that was created, that’s a little more difficult and you will have to use the trick described in the same link to do that “Removing Repeat Groups (Mobile)”. if you wish to hide/show questions inside the repeat based on an answer inside the same repeat, then just add a questions list and use the displace conditions there. Display conditions are great for stuff like this.
if you wish for the repeat groups to be automatically created, then i suggest:
create a hidden calculation that uses that criteria you want, to determine the number of repeats that you actually need, and use that hidden calculation as the number of repeats (repeat group settings). and also use the trick to remove repeat groups so that it applies the filters you need and hide the groups you don’t actually want to show up. it’s a little iffy and would need you to test out a bunch of different scenarios, but it’ll do the trick.
if you wish to perform an operation on each iteration inside a repeat, then you will likely need to have the calculation live inside the repeat itself. if the repeated calculation is supposed to consider elements that are outside the repeat, you can reference those directly, but if it needs to consider other elements in the same repeat, i advise you to use the aggregate functions that are available such as “count” and “sum” and to also leverage the position() function where needed. you just might have to cut your logic into many smaller pieces and perform it in steps.