FOR


The FOR statement allows the programming of looping constructs within the program. The loop is controlled by a counting variable and may be terminated early by expressions tested after every iteration.

 

COMMAND SYNTAX

FOR var=expression1 TO expression2 {STEP expression3} {WHILE | UNTIL expression4}...NEXT {var}

 

SYNTAX ELEMENTS

var is the counting variable used to control the loop. The first time the loop is entered var is assigned the value of expression1, which must evaluate to a numeric value. After every iteration of the loop var is automatically incremented by 1. expression2 must also evaluate to a numeric value as it causes the loop to terminate when the value of var is greater than the value of this expression. expression2 is evaluated at the start of every iteration of the loop and compared with the value of expression1.

If the STEP expression3 clause is included within the statement, var will automatically be incremented by the value of expression3 after each iteration of the loop. expression3 is evaluated at the start of each iteration. expression3 may be negative, in which case the loop will terminate when var is less than expression2.

The statement may optionally include either a WHILE or UNTIL clause (not both), which will be evaluated before each iteration of the loop. When the WHILE clause is specified the loop will only continue with the next iteration if expression4 evaluates to Boolean TRUE. When the UNTIL clause is specified the loop will only continue with the next iteration if expression4 evaluates to Boolean FALSE.

 

NOTES

Because expression2 and expression3 must be evaluated upon each iteration of the loop you should only code complex expressions here if they may change within each iteration. If the values they yield will not change then you should assign the value of these expressions to a variable before coding the loop statement. Expressions 3 and 4 should then be replaced with these variables. This can offer large performance increases where complex expressions are being used.

See also: BREAK, CONTINUE.

 

EXAMPLES

Max =DCOUNT(BigVar, CHAR(254))
FOR I = 1 TO Max STEP 2 WHILE BigVar LT 25
   BigVar += 1
NEXT I

This example will increment every second field of the variable BigVar but the loop will terminate early if the current field to be incremented is not numerically less than 25.


jBC