# for i in range loop

I tried to build a for loop with an range. It looked easy for me, because I just would utilize the the existing "for i = start to end" loop and just pass the parameter to the lambda. After spending several hours and having not enough sleep I must eat my anger and my pride and confess I didnt make it. So I can anybody please tell me where I went wrong ???

In the for ... to loop, j is incremented. Nowhere in the script is i incremented. Put set i to j in the for ... to loop.

Hi Matthias, it seems to me that you're really trying to implement the for each block, so you might as well use it directly:

If you want it to be yellow, you could wrap your own custom block around it, such as this:

or, if you wan to "go all the way" you could also implement more parts, for example like this:

or - perhaps even more elegantly - like this:

Hi. @snapenilk has correctly pointed to the source of your bug. I'd like to expand upon that by taking note of your RUN (block) WITH INPUTS (i). Unless you want your version to behave differently, the script in the C-slot of a FOR doesn't take an input. That is, you don't leave an input slot empty in the script, as you do in the higher order functions MAP etc. So that instruction should just say RUN (block). In your inner (primitive) FOR, its own upvar j will be incremented each time through the loop, but the user isn't using that upvar in the script! The user is using the outer one, i. So you have to SET (i) TO (j) before the RUN.

Why do you use CHANGE rather than SET when you're initializing BEGIN and END? It works, because script vars are initialized to 0, but it led me to wonder if I'd missed an earlier SET.

But the big question in my mind is, what do you want your block to do if someone calls it with a list whose items aren't a consecutive range of integers? Or not integers at all? It seems roundabout to construct a list of numbers only to ignore all but the first and last of the list, which are precisely the inputs to RANGE. The RANGE block might as well just return a two-item list containing its inputs! That's why, as Jens said, you're better off using FOR EACH ITEM instead of numeric FOR.

THANK you all for your replies.
It’s a bit embarrassing, now that I understand where I went wrong with my confused thoughts.
But it’s OK to be confused. Confusion is the route to all the clarity in the world.