FOR loop fails to stop

In a FOR loop, when the second parameter contains a value less than or equal to -1 and the third parameter contains an end value of -1, the loop does not stop when the end value reaches -1. It loops endlessly unless a stoploopif is present.

FOR x,-3,-1
message x
stoploopif x=2
endloop

For all other end values I have tested, like -3,-2, 0 and 1, for example, the loops stop as expected if the start value is less than or equal to the end value.

Looks like a bug.

David Duncan

The for loop statement was designed for use with positive integer values. The documentation needs to be changed to reflect that.

That’s a shame.

Negative numbers in the FOR loop allow use of a descending loop number to work from the highest index numbers of a list to the lowest, for example when importing the contents of a mail folder, the oldest (highest numbered) email first. This can be done otherwise with a different loop and more steps, but the FOR loop is most elegant.

I am using this technique in a few places and would like to continue.

(Of course, by adding a stoploopif x=-1 the FOR loop stops with an end number of -1.)

You could still use a For loop by writing something like

For x, 0, Last-1
    message Last-x
endloop

That would give you a countdown from Last to 1.

Very nice, Dave, thank you. (But not as easy to figure out what’s going on as negative values are, I think.)