[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
+---------------------------------+
|         FOR ... ENDFOR          |
+---------------------------------+
FOR <memvar> = <expN1> TO <expN2>
        [STEP <expN3>]
                <statements>
        [EXIT]
        [LOOP]
ENDFOR | NEXT

-----------------------------------
Executes statements in loop specified number of times.
-----------------------------------

The FOR ... ENDFOR command executes a set of statements within a loop a
specified number of times.  A memory variable <memvar> is used as a
counter to determine how many times the statements inside the loop will
be executed.

The memory variable <memvar> need not be defined before executing FOR
... ENDFOR.  The first numeric expression <expN1> is the initial value
of the counter, and the second numeric expression <expN2> is the final
value of the counter.

Program statements after FOR are executed until an ENDFOR or NEXT is
reached.  The counter <memvar> is then incremented by the value of STEP
<expN3>.  If the STEP option is not included, the counter is incremented
by one.

The counter is then compared with the final value <expN2>.  If the
counter is less than or equal to the final value <expN2>, the statements
following FOR are executed again.  If the counter is greater than the
final value <expN2>, execution branches outside of the FOR ... ENDFOR
loop and continues with the command immediately following ENDFOR.

The EXIT command transfers control from within the FOR ... ENDFOR loop
to the command immediately following ENDFOR.  EXIT may be placed
anywhere between FOR and ENDFOR.  LOOP may also be placed anywhere
between FOR and ENDFOR.  Including  the LOOP keyword causes control to
return directly back to FOR (the counter is incremented as if the ENDFOR
were reached).  FOR ... ENDFOR loops may be nested.

Note that comments may be placed after STEP <expN> and ENDFOR (or NEXT)
on the same line -- the comments are ignored during program compilation
and execution.

Clauses
-------

<memvar> = <expN1> TO <expN2>
        <memvar> is memory variable that acts as counter; <expN1> is initial
value of counter; <expN2> is final value of counter.  The numeric
expressions <expN1>, <expN2> and <expN3> may be numeric expressions or
numeric memory variables.  The values of <expN1>, <expN2> and <expN3>
are only read initially, and their values may be changed inside of the
loop.  Changing their values inside the loop will not change the number
of times the loop is executed.  Exercise caution if you change the value
of the counter <memvar> inside the loop.

STEP <expN3>
        Amount counter <memvar> is incremented by.  If the value of the STEP
<expN3> is negative, the memory variable counter is decremented
(decreased) each time through the loop, and the initial value <expN1>
must be greater than the final value <expN2>.

<statements>
        FoxPro commands to be executed.

EXIT
        Transfers control in FOR ... ENDFOR loop to command immediately
following ENDFOR before <memvar> reaches its final value.

LOOP
        Causes control to return directly back to FOR without executing
statements between LOOP and ENDFOR.

+---------------------------------+
|             Examples            |
+---------------------------------+
In first example, numbers 1 through 10 are listed.  Second example
demonstrates use of negative STEP value.  Numbers 10 through 1 are
listed.  Last example shows that initial, final and STEP values can be
memory variables.  This example displays all even numbered records from
2 through 10 in CUSTOMER database.

CLEAR
FOR mcount = 1 TO 10
        ? mcount
ENDFOR

FOR mcount = 2 * 5 TO 1 STEP -1
        ? mcount
ENDFOR

SET TALK OFF
USE customer
GO TOP
STORE 2 TO I
STORE 10 TO J
STORE 2 TO K
FOR mcount = I TO J STEP K
        GOTO mcount
        DISPLAY UPPER (company)
ENDFOR
USE  && close database

-----------------------------------

See Also:  DO CASE, DO WHILE, SCAN

-----------------------------------

See Also: DO CASE DO WHILE SCAN
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson