0 Favourites

# reverse for loop

This forum is currently in read-only mode.
• 8 posts
• Is there any way of doing a for loop in reverse i.e for x =10 to 0.I have tried but can't get it to work.

• Are you trying to use loopindex as an expression? Because you could use 10-loopindex (total loops-current loop).

• Is there any way of doing a for loop in reverse i.e for x =10 to 0.I have tried but can't get it to work.

It doesn't work

In the past I made a request to add a "step" feature to loops like in BASIC so you could do something like this:

For "X" = 10 to 1 step -1

(counts backward by 1)

or

For "X" = 25 to 50 step 5

(counts forward by 5)

etc.

Also the loops only take intergers so you wouldn't be able to go from 10 to 0.1 anyway.

• some simple math on the loop index would do the trick

desiredindex = desiredstart+(((loopindex-start)/(end-start))*(desiredend-desiredstart))

that means if you loop from 1 to 10 and you really wanted from 1 to 2 in steps of 0.1

di = 1+( ((loopindex-1)/(10-1)) * (2-1) )

di = 1+((loopindex-1)/9)

if you loop from 1 to 10 and really wanted 10 to 1

di = 10+( ((loopindex-1)/(10-1)) * (1-10) )

di = 10+( ((loopindex-1)/9) * (-9) )

at loopindex 1 this is 10+(((1-1)/9)*(-9)) = 10

at loopindex 2 this is 10+(((2-1)/9)*(-9)) = 10+( (1/9) * (-9) ) = 10-1 = 9

at loopindex 10 this is 10+(((10-1)/9)*(-9)) = 10+( -9 ) = 1

PS: Or you could start all your loops from 0 to n-1 where n is the number of steps you want, then use the linear interpolation function that's built into construct to interpolate between the start of the sequence you want to the end. In fact, do that, it's the same only faster and simpler.

• You could easily use a private variable, and probably a function would do the trick as well.

But for ease of use I would really like to see what deadeye mentioned implemeted.

• <img src="http://files.getdropbox.com/u/1013446/looping.PNG">

• I'd just do a for 1 to 10 and then use 10 - LoopIndex. Dusted!