Download

Problem with ForLoop output

Hello, I am trying to print to the screen every digit between 0 and 361. For some reason, even though I have the first index as 0, and the last index as 360, it is only outputting 361, and not any number in between. I put a Delay in the loop body to try to slow it down so that the numbers can be seen but even that won’t work. I know I’m doing something dumb, I just can’t figure it out. Can anyone explain why this is happening? I need this loop to work for the rotation of a certain object, but I can’t understand this ForLoop. Thanks. Please Refer to the attached Image.

Hmm yeah that’s a bit weird.

But if I don’t use the delay it works… you should open the output log instead of relying in the screen message then you will see all the numbers printed.

ForLoops + delays is probably not a good idea anyway :p.

Your problem is that the for loop takes less then .1 seconds to complete and then when your delay is finished the index it uses is already at 361 as the print string gets called after the delay and therefor it takes the current index after .1 seconds and this is already 361 as the loop is finished within .1 seconds.
Hope that was understandable.

Maybe I don’t understand forloops correctly, but isn’t it supposed to execute the body, in this case the delay, before going to the next iteration?

That is probably true for every node except delay.

Loops don’t handle delays or timers, what you need is a DoN. You put the delay before DoN if you need it, you will need a branch or event to reset the counter

I just find this so strange, because it is different from for loops in real programming. for example when you have for(i=0;i<360;i++) {print (i); delay(1sec);} the for loop won’t increment until whatever is between the brackets has been executed. but yea thanks DEDRICK the DoN is a good idea, I would have never thought of it.