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. |
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 |