For

This command creates a counted loop. The commands inside the loop are executed a set number of times. Don't confuse this command with the FOR clause that's permitted on many commands. (See "Scope, FOR, WHILE and Santa Clauses" for more on that one.)

Usage

FOR CounterName = nStart TO nStop [ STEP nIncrement ]
    [Commands]
    [EXIT]
    [LOOP]
ENDFOR | NEXT

Parameter

Value

Meaning

CounterName

Numeric

The loop counter. It's initially assigned nStart, then changed by nIncrement each time through until it passes nStop. Created if it doesn't exist.

nStart

Numeric

The starting point of the loop.

nStop

Numeric

The ending point of the loop.

nIncrement

Omitted

Increase CounterName by 1 each time through the loop.

Numeric

Indicates how much CounterName changes by each time through the loop.

Commands

Any Visual FoxPro commands

The command(s) to be executed each time through the loop.


nIncrement can be positive or negative. It doesn't have to be an integer, so you can increment by .5 or 2.7395, if that's what you need. If nIncrement is negative, nStart is normally larger than nStop. The loop ends when CounterName passes nStop (in the direction determined by nIncrement)—CounterName doesn't ever have to exactly equal nStop.Watch out for one subtle point. If nStart and nStop are equal, the commands inside the loop execute exactly once. If nStart is greater than nStop and nIncrement is positive, or nStart is less than nStop and nIncrement is negative, the commands don't execute at all.Any of nStart, nStop and nIncrement can be expressions. But the expression is evaluated only once—when you first reach the loop. The boundaries and the increment are "burned in" and don't change, no matter what you do to the variables involved inside the loop.You can change the value of CounterName inside the loop, but we don't recommend it. It's the road to a maintenance nightmare.LOOP and EXIT allow you to short-circuit the loop. LOOP bails out of the current pass through the loop, and goes back to increment the counter and try again. EXIT bails out of the loop entirely, continuing with the next command after ENDFOR. Structured programming theory says that every construct in a program should have one entrance and one exit. Both LOOP and EXIT violate this rule. Since neither one is necessary (you can always set a logical flag and use IF to skip any commands you want to), we recommend you use them very sparingly. We avoid LOOP entirely; EXIT is useful because it allows some loops to be written as FOR loops rather than DO WHILE. There's an example of this below.It's a little-known fact that you can end a FOR loop with NEXT rather than ENDFOR. Unlike Basic's NEXT, you don't repeat the loop counter, though, as in NEXT X.VFP 5 added the FOR EACH command, which lets you loop through all the elements of a collection or array without using a counter. It's a good alternative to FOR in a number of situations.

Example

* Get a list of tables
nDBFCount = ADIR(aDBFs,"*.dbf")
 
* Now print them out
FOR nCnt = 1 TO nDBFCount
    ? aDBFs[nCnt, 1]
ENDFOR
 
* Since the array created by ADIR() has 5 columns, the STEP 
* clause provides an alternate way to write the loop above.
* Since the output of ADIR() could change in future versions,
* the code above is preferred, though
FOR nCnt = 1 TO ALEN(aDBFs) STEP 5
    ? aDBFs[nCnt]
ENDFOR
 
* Add up values in an array (aValues) until you top 1000
* This example can be done with FOR or DO WHILE. 
* Both versions are here.
 
* FOR version
nTotal = 0
FOR nCnt = 1 TO ALEN(aValues)
    nTotal = nTotal + aValues[nCnt]
    IF nTotal > 1000
        EXIT
    ENDIF
ENDFOR
 
* DO WHILE version
nTotal = 0
nCnt = 1
DO WHILE nCnt <= ALEN(aValues) AND nTotal <= 1000
    nTotal = nTotal + aValues[nCnt]
ENDDO

See Also

Do While, Exit, For Each, If, Loop, Scan


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .