Host Engineering Forum

General Category => Do-more CPUs and Do-more Designer Software => Topic started by: Controls Guy on April 04, 2022, 03:40:33 PM

Title: Manipulation of loop index inside loop
Post by: Controls Guy on April 04, 2022, 03:40:33 PM
I have a loop that depending on user settings can iterate from a negative number, say -1000 up to around 500.   If a certain relay is set, I want to override the user setting and skip the negative values and start iterating from an index of 0.  I assume that I can just check inside the loop and if that relay is set and the index is negative, just write 0 directly to the index?    That way on the first iteration I'd skip from -1000 to 0 before executing the internals of the loop.

Does the loop just add the step value to the index on each iteration so that me writing to the index inside the loop but outside the FOR box won't confuse it?  Or do I need to do a conditional MATH to compute the correct starting index before entering the loop?
Title: Re: Manipulation of loop index inside loop
Post by: franji1 on April 04, 2022, 05:13:09 PM
That should work.  Say the index is D0 (or N0, but not V0) and "step" is 100:

FOR D0 -1000 500 100
// first rung after FOR!?!
STR Override
AND D0 < 0
MOVE 0 D0

// more rungs

NEXT

Realize it would be quicker if you used a variable for the START INDEX (-1000 vs. 0) because that would be done ONCE, BEFORE the FOR loop.  Otherwise, you will be executing that first rung EVERY time through the loop.

// set D1 to the START index
STR $On
MATH D1 "IF(Override, 0, -1000)"

FOR D0 D1 500 100
// just original rungs
NEXT

Title: Re: Manipulation of loop index inside loop
Post by: Controls Guy on April 04, 2022, 06:12:08 PM
That's a good point.   I was shying away from that method because the rung in question got more complex, but yeah, executing it every iteration isn't worth it.