Aracılığıyla paylaş


FOR ... ENDFOR Command

Executes a set of commands a specified number of times.

FOR VarName = nInitialValue TO nFinalValue [STEP nIncrement] 
      Commands
   [EXIT]
   [LOOP]
ENDFOR | NEXT

Parameters

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

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

  • [EXIT]
    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.

  • [LOOP]
    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.

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

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

Remarks

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.

Note

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

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
ENDFOR

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.

OPEN DATABASE (HOME(2) + 'Data\TestData')
USE Customer  
FOR gnCount = 1 TO 10 STEP 2
   GOTO gnCount  
   DISPLAY Company 
ENDFOR

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

See Also

Reference

DO CASE ... ENDCASE Command

DO WHILE ... ENDDO Command

FOR EACH ... ENDFOR Command

SCAN ... ENDSCAN Command

Other Resources

Commands (Visual FoxPro)

Language Reference (Visual FoxPro)