for
for
VARIABLE
,
START
,
END

The for statement is used at the beginning of a loop that loops a fixed number of times.

Parameters

This statement has three parameters:

variable – is the name of a field or variable where the loop count will be stored. If this field or variable does not already exist it will be created as a local variable.

start – is the starting value of the loop. This value is calculated once before the loop starts, it cannot change once the loop begins running.

end – is the ending value of the loop. This value is calculated once before the loop starts, it cannot change once the loop begins running.

Description

The for statement is used at the beginning of a loop that iterates a fixed number of times (see the loop statement for more information about loops). For loops are always terminated with an endloop statement.

The first parameter of the for statement is the name of a field or variable. Within the loop, this field or variable will contain the number of times the loop has repeated. After the loop has finished, this field or variable will contain the number of times the loop actually repeated.

This simple example adds 10 new records to the current database. The Sequence field will contain a number from 1 to 10 (1 in the first record added, 10 in the last record added).

    for i,1,10
        addrecord
        Sequence=i
    endloop

The loop does not have to begin with 1 – it can begin with any positive integer value (negative values are not allowed).

    for i,11,20
        addrecord
        Sequence=i
    endloop

The start and end values can be calculated with formulas, like this:

    local start,count
    start=101
    count=100
    for i,start,start+count-1
        addrecord
        Sequence=i
    endloop

Keep in mind that these formulas are calculated only once when the loop starts. In this silly example the count value changes inside the loop. In spite of this, the loop will always run 100 times, not 50 or 51.

    local start,count
    start=101
    count=100
    for i,start,start+count-1
        addrecord
        Sequence=i
        count=50
    endloop

You can nest one for loop inside another. If you do, you will probably want to use a different variable for each of the loops. (Advanced tip: The loops will still work if the same variable is used for multiple nested loops, but the variable may not always contain the value you expect.)

    local multiplicationTable
    multiplicationTable =""
    for i,1,10
        for j,1,10
            multiplicationTable=multiplicationTable+cr()+str(i)+"*"+str(j)+"="+str(i*j)
        endloop
    endloop
    message multiplicationTable

Error Messages

LOOP without ENDLOOP – Each FOR statement must be paired with a corresponding ENDLOOP, UNTIL or WHILE statement. If this error message appears, the necessary statement terminating the loop has not been included.

See Also

- endloop -- is used at the end of a loop.
- info("timelimitremaining") -- returns the amount of time available (in seconds) before a loop will time out.
- loop -- is used at the beginning of a loop.
- looparray -- is used at the beginning of a loop that loops over the elements of an array.
- loopdataarray -- is used at the beginning of a loop that loops over the elements of a data array.
- loopindex -- allows a procedure to determine how many times a loop has been repeated.
- loopwhile -- is used at the beginning of a loop.
- Preventing Endless Loops -- setting up a timeout limit to prevent endless loops.
- repeatloopif -- decides whether to continue with a loop or to start over again from the top.
- stoploopif -- decides whether to continue with a loop or to exit the loop immediately.
- timelimit -- specifies the maximum time a procedure can continue to run after this statement.
- until -- is used at the end of a loop, and can control how many times the loop is executed.
- while -- is used at the end of a loop, and can control how many times the loop is executed.

History

Version | Status | Notes
10.0 | New | New in this version