QTIPS - FOR/NEXT variables

Published ByDateVersionKnowledge LevelKeywords
Sprezzatura Ltd01 JUN 19922.12+EXPERTFOR, NEXT, STEP

Tony McDowell writes - "The documentation (page N4.73) states that "start", "end" and "step" must be integers. A little experimentation might suggest that in fact any decimal fractions can be used for these variables. Don't be fooled. If STEP is a decimal fraction then anomalous results can occur. Specifically, it seems that the "even-ness" of the bound and step variables is critical. My tests show that if step and the lower and upper bounds are "even" decimal fractions then the FOR/NEXT loop will incorrectly terminate when step is equal to "end" rather than when it exceeds "end" (or underflows if step is negative)."

     * This one is bad - it won't hit -2.0
     A = 2 ; B = -2
     For I = A To B Step -0.2 ; Call Msg("I = " : I) ; Next

     * This one is OK
     A = 1 ; B = -1
     For I = A To B Step -0.2 ; Call Msg("I = " : I) ; Next

     * This one OK
     A = 2 ; B = 4
     For I = A To B Step -0.5 ; Call Msg("I = " : I) ; Next

     * This one is bad - it won't hit 4.0
     A = 2 ; B = 4
     For I = A To B Step -0.4 ; Call Msg("I = " : I) ; Next

* NB Version 2.12 MSG used above!

(Volume 4, Issue 2, Page 5)