For statement (LotusScript® Language)

Executes a block of statements a specified number of times.

Syntax

For countVar = first To last [ Step increment ]

[ statements ]

Next [ countVar ]

Elements

countVar

A variable used to count repetitions of the block of statements. The data type of countVar should be numeric.

first

A numeric expression. Its value is the initial value of countVar.

last

A numeric expression. Its value is the final value of countVar.

increment

The value (a numeric expression) by which the countVar is incremented after each execution of the statement block. The default value of increment is 1. Note that increment can be negative.

Usage

After exit from a loop, the countVar for the loop has its most recent value.

Executing the loop the first time

Before the block of statements is executed for the first time, first is compared to last. If increment is positive and first is greater than last, or if increment is negative and first is less than last, the body of the loop isn't executed. Execution continues with the first statement following the For loop's terminator (Next).

Otherwise countVar is set to first and the body of the loop is executed.

Executing the loop more than once

After each execution of the loop, increment is added to countVar. Then countVar is compared to last. When the value of countVar is greater than last for a positive increment, or less than last for a negative increment, the loop is complete and execution continues with the first statement following the For loop's terminator (Next). Otherwise the loop is executed again.

Exiting the loop early

You can exit a For loop early with an Exit For statement or a GoTo statement. When LotusScript® encounters an Exit For, execution continues with the first statement following the For loop's terminator (Next). When LotusScript® encounters a GoTo statement, execution continues with the statement at the specified label.

Nested For loops

You can include a For loop within a For loop, as in the following example:

Dim x As Integer
Dim y As Integer
For x% = 1 To 3
   For y% = 1 To 2
      Print x% ; 
   Next        ' Next y
Next           ' Next x
' Output: 1 1 2 2 3 3

If you don't include countVar as part of a For loop terminator (Next), LotusScript® matches For loop delimiters from the most deeply nested to the outermost.

LotusScript® lets you combine For loop terminators when they are contiguous, as in the following example:

Dim x As Integer
Dim y As Integer
For x% = 1 To 3
   For y% = 1 To 2
      Print x% ;
Next y%, x% 'Terminate the inner loop and then the outer loop.
' Output: 1 1 2 2 3 3

Example