Anemone Loop not counting right?

Hi everyone.

I’m trying to understand how to do Loops in Grasshopper.
I’m using Anemone’s Loop component.

I want to take number A and subtract it by number B.
If A is smaller than B, the loop should exit.
Otherwise, we subtract them.

The problem is, the loop is not exiting when A is smaller than B. It runs one extra time.

The expected result would be the loop to stop when A is smaller than B, but it runs one more time and we get that negative number.

The same happens when A is not divisible by B, or when A is already smaller than B from the start.

I tried to use if statements and stream gates but it only made it worse as I don’t have a solid understanding of the basics of such components.
Can anyone point me to the right direction?
It looks like something really stupid that I’m failing to understand :sweat_smile:

Thank you!

I just realized I didn’t added the file in the first comment.
Here it goes. (9.5 KB)


I fixed the spelling in your title and tag.

Thank you @martinsiegrist

1 Like

You did great, and the loop is right.
What happens is that first the number goes into loop → and then it checks if it meets the condition. Therefore you will be left in this case with the number that did not meet the condition you placed.

One solution is to remove the last branch from the list.

Thanks for the reply !

This totally works!
The only problem is in the situation where number A is smaller than number B from start, it ends up giving me a “No Data was collected” error at the end, and I end up with no “remainder”, and no way of getting number A in the end.

If there is a way of using Anemone’s Loop exiting before performing the subtraction, then we wouldn’t need to do that trick removing the last item after the Loop, and I believe it would solve the problem?

If not… any ideas on how to make a workaround this issue?

Something like that can work ?

I canceled the recording of the loop and now it is done inside the loop, this way you can control the output.
I also added at the beginning of the loop, a condition that checks if the first number meets the condition.
Note that another parameter needs to be inserted for the loop.