FOR ... ENDFOR Command
Collapse the table of content
Expand the table of content

FOR ... ENDFOR Command

Executes a set of commands a specified number of times.

FOR VarName = nInitialValue TO nFinalValue [STEP nIncrement] 



Specifies the name of the variable to act as the counter. The counter keeps track of the number of times the Visual FoxPro commands execute inside the FOR ... ENDFOR loop. The variable does not need to exist before executing FOR ... ENDFOR.

nInitialValueTO nFinalValue

Specifies the initial and final values of the counter. Both nInitialValue and nFinalValue can be array elements.

[STEP nIncrement]

Specifies the amount to increment or decrement the counter value. If nIncrement is negative, the counter value is decremented. If you omit the STEP clause, VarName increments by 1.


Specifies the Visual FoxPro commands to execute. Commands can include any number of commands.


Transfers control from within the FOR ... ENDFOR loop to the command immediately following ENDFOR. You can place EXIT anywhere between FOR and ENDFOR. For more information, see EXIT Command.


Returns control to the FOR clause without executing the statements between the LOOP and ENDFOR keywords. The counter value increments or decrements as if ENDFOR was reached. For more information, see LOOP Command.


Specifies the end of the FOR ... ENDFOR loop.


Specifies the location to continue program execution after the counter value exceeds nFinalValue.

The Visual FoxPro commands that appear in the FOR loop execute until ENDFOR or NEXT is reached. The value in the counter VarName then increments or decrements by the value of nIncrement. The value of the counter is then compared with nFinalValue. If the counter is less than or equal to nFinalValue, the commands following the FOR clause execute again. If the counter is greater than nFinalValue, the FOR ... ENDFOR loop exits, and program execution continues with the first command following ENDFOR or NEXT.


The values of nInitialValue, nFinalValue, and nIncrement are read only initially. However, changing the value of the counter VarName inside the loop affects the number of times the loop is executed. Changing the value of nFinalValue in a FOR loop has no effect.

Example 1

In the following example, the FOR ... ENDFOR loop displays the numbers 1 through 10 using the ? command:

FOR gnCount = 1 TO 10
   ? gnCount

For more information, see ? | ?? Command.

Example 2

The following example opens Customer table in the Visual FoxPro sample TestData database, located in the Microsoft Visual FoxPro directory ..\Samples\Data. The FOR ... ENDFOR loop specifies an initial value of 1, an increment value of 2, and a final value of 10. The GOTO command moves the record pointer to the record number specified by the variable gnCount and the DISPLAY command displays the name of the company from the Company field in the table. The FOR ... ENDFOR loop displays all odd-numbered records from the first 10 records.

USE Customer  
FOR gnCount = 1 TO 10 STEP 2
   GOTO gnCount  
   DISPLAY Company 

For more information, see GO | GOTO Command and DISPLAY Command.

Community Additions

© 2015 Microsoft