Executes the statements between FOR and ENDFOR the number of times indicated by the FOR statement.

Syntax

FOR <memvar> = <start expN> TO <end expN> [STEP <step expN>]
 [ <statements> ]
ENDFOR | NEXT

<memvar>

The loop counter, a memory variable that's incremented or decremented and then tested each time through the loop.

<start expN>

The initial value of <memvar>.

<end expN>

The final allowed value of <memvar>.

STEP <step expN>

Defines a step size (<step expN>) by which dBASE Plus increments or decrements <memvar> each time the loop executes. The default step size is 1.

When <step expN> is positive, dBASE Plus increments <memvar> until it is greater than <end expN>. When <step expN> is negative, dBASE Plus decrements <memvar> until it is less than <end expN>.

<statements>

Zero or more statements executed in each iteration of the loop.

ENDFOR | NEXT

A required keyword that marks the end of the FOR loop. You may use either ENDFOR (more dBASE-ish) or NEXT.

Description

Use FOR...ENDFOR to execute a block of statements a specified number of times. When dBASE Plus first encounters a FOR loop, it sets <memvar> to <start expN>, and reads the values for <end expN> and <step expN>. (If <end expN> or <step expN> are variables and are changed inside the loop, the loop will not see the change and the original values will still be used to control the loop.)

The loop counter is checked at the beginning of each iteration of the loop, including the first iteration. If <memvar> evaluates to a number greater than <end expN> (or less than <end expN> if <step expN> is negative), dBASE Plus exits the FOR loop and executes the line following ENDFOR (or NEXT). Therefore, it’s possible that the loop body is not executed at all.

If <memvar> is in the range from <start expN> through <end expN>, the loop body is executed. After executing the statements in the loop, <step expN> is added to <memvar>, and the loop counter is checked again. The process repeats until the loop counter goes out of range.

You may also exit the loop with EXIT, or restart the loop with LOOP.

The <memvar> is usually used inside the loop to refer to numbered items, and continues to exist after the loop is done, just like a normal variable. If you do not want the variable to be the default private scope, you should declare the scope of the variable before the FOR loop.