For...Next loops

The iterative block statement For executes a block of statements a specified number of times.

The syntax is:

For countVar = first To last [ Step increment ]

[ statements ]

Next [ countVar [ , countVar ]... ]

This example shows a For statement that does not use the Step or Next optional items.

Dim power2 As Integer
For iV = 1 To 15 
   power2 = 2 ^ iV - 1
   Print power2% ;
' Output:
' 1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767

The first line of the For statement in the previous example is equivalent to this code:

For iV = 1 To 15 Step 1

That is, if the phrase Step increment is omitted from the statement, the default value of increment is 1.

The body of the For statement can be empty: there need be no statements at all between For and Next.

Variables in the control expressions: their data type and declaration

If any variables appear in the control expressions first, last, or increment, LotusScript® uses their current values. If they were not previously declared or used, LotusScript® implicitly declares them as Variants and initializes them to EMPTY. You must be certain that any variables in these expressions have been declared before executing the For statement.

LotusScript® initializes the counter variable to the value of first when the For statement is entered. If countVar was not previously declared or used, LotusScript® declares it as a Variant. (Note that if your script includes the Option Declare statement, then countVar must be declared before you use it in a For statement.) You should always declare your loop variable: additional computing resources are necessary to convert the value to a Variant in a tight loop.

For example:

' If the variable iV was not previously declared or used,
' this For statement declares it as a Variant.
' Its value after the For statement completes execution is the
' last value assigned to it during the For statement 
' execution (16).
For iV = 1 To 15
Print TypeName(iV), iV
iV = "abc"
Print TypeName(iV), iV 
' Output:
' INTEGER       16
' STRING       abc

In this example, a compiler error results when you attempt to use 2 ^ 15 as the limiting value for an Integer counter variable in a For statement. This is because the maximum Integer value in LotusScript® is (2 ^ 15) - 1.

Dim i As Integer
For i% = 1 To 2 ^ 15
' Output:
' Error 6: Overflow

When the counter variable is a Variant, LotusScript® converts its value to the appropriate data type when it executes the For statement.

For example:

For iV = 1 To 2 ^ 15
Print TypeName(iV), iV
' Output:
' LONG    32769

This example is similar:

' The Variant kV has a Double value in every iteration of 
' this loop, because the For statement first assigns it 
' the Double value 1.0 and thereafter adds 1 to the value
' in each iteration.
For kV = 1.0 To 3
Print TypeName(kV), kV
' Output:
' DOUBLE   4

In this example, the value of kV during the second iteration of For is the Double value 2.1:

' This loop iterates only twice because the third value
' of kV is 3.2, which is larger than the limiting value, 3.
For kV = 1 To 3 Step 1.1       
   Print TypeName(kV), kV
' Output:
' DOUBLE   2.1

The LotusScript® data type conversion rules apply to the counter variable.

For example:

' In this instance, the Step value, 1.1, is rounded to the
' Integer value 1 each time it is used to increment k%, 
' because k% is declared as an Integer variable.
Dim k As Integer
For k% = 1 To 3 Step 1.1      
   Print TypeName(k%), k%
' Output:  
' INTEGER       1
' INTEGER       2
' INTEGER       3

Nested For statements

The example illustrates the usefulness of nested For statements. The example computes and prints the binomial coefficients (denoted mathematically b(j; k)) for every integer k from 1 to n, for any positive integer n. The algorithm used is the Pascal triangle method, by which b(j; k) is calculated as the sum b(j - 1; k - 1) + b(j - 1; k).

In this example, three separate For statements are nested inside an outer For statement.

Sub CoArray(n As Integer)
   Dim i As Integer, j As Integer, k As Integer 
   Dim coHold() As Double, coCalc() As Double
   ' Initialize arrays coHold and coCalc to 0.
   ' Alternate elements within each of these arrays will
   ' always be 0. The coefficients are stored in coCalc by
   ' addition from coHold.
   ReDim coHold(2 * n%)
   ReDim coCalc(2 * n% + 1)
   coHold(n%) = 1
   Print "Binomial coefficients for the integers up to:" n%
   ' Each iteration of this outer For statement "For j% ..."
   ' computes a line of coefficients.
   For j% = 0 To n%
      If j% > 0 Then
         ' The statement "For k%..." creates an array
         ' of coefficients in the middle of array coCalc.
         ' Alternate elements in this part of coCalc
         ' remain 0, and the ends of coCalc remain 0.
         For k% = n% - j% + 1 To n% + j% - 1
            coCalc(k%) = coHold(k% - 1) + coHold(k% + 1)
         Next k%
      End If
      ' Set the 0-th and j-th coefficients to 1.
      coCalc(n% - j%) = 1
      coCalc(n% + j%) = 1
      Print "Coefficients for j = "j%":";
      ' The statement "For k% ..." writes the new coefficients
      ' back into coHold to be used the next time around.
      For k% = n% - j% To n% + j%
         coHold(k%) = coCalc(k%)
      Next k%
      ' This For statement prints the line of coefficients for 
      ' this value of j%. Every 2nd element of coCalc is 0. 
      ' Don't print 0's.
      For k% = 0 To 2 * n%
         If coCalc(k%) > 0 Then Print coCalc(k%);
      Next k% 
   Next j%
End Sub
Call CoArray(5)
' Output:
' Binomial coefficients for the integers up to: 5
' Coefficients for 0 : 1
' Coefficients for 1 : 1  1
' Coefficients for 2 : 1  2  1
' Coefficients for 3 : 1  3  3  1
' Coefficients for 4 : 1  4  6  4  1
' Coefficients for 5 : 1  5  10  10  5  1

You can call the sub CoArray with larger argument values to obtain other sets of binomial coefficients.

Other features of this algorithm are:

  • To print the coefficients only for n, rather than for every integer up to n, move the final nested For statement (For k% = 0 To 2 * n...) outside of the current outer For statement (For j% = 0 To n...), after the phrase Next j%.
  • For small values of n, the algorithm is the easiest way of computing and writing out all of these binomical coefficients by hand in a symmetric triangular array, where the longest, last row contains the coefficients for n itself. Each coefficient is the sum of two coefficients already computed: its "northwest" and "northeast" neighbors in the array. For n = 15, the left half of the array can be produced by hand addition in a minute or so; one half is its mirror image.
  • If the factorials of 1 through n are known, they can be used to compute the binomial coefficients. If a function to compute the factorial is called FactNum, then a binomial coefficient b(n; k) can be expressed as:
    FactNum(n%) / (FactNum(k%) * FactNum(n% - k%))

    This is a more conventional way of computing the coefficient. You can write a routine using FactNum to compute and print the same set of coefficients generated by the sub CoArray in the preceding example. FactNum itself can be written as a function using a For statement:

    Function FactNum(n As Integer) As Double
       FactNum# = 1
       For i% = 1 To n%
          FactNum# = FactNum# * i%
       Next i%
    End Function

    Each method has its advantages:

    • The formula using FactNum is the definition of the binomial coefficient, so that routine may be easier to read and modify.
    • The implementation by CoArray is fast, and involves no calls to other routines. CoArray can take larger arguments than FactNum, since the largest number CoArray computes is a coefficient, rather than the factorial of n.

The definition of the sub CoArray ends with two Next statements that complete two For statements. You can rewrite the Next statements in this way:

Next k%
Next j%

That is, k% and j% are optional in these statements. This code is also equivalent:

Next k%, j%

When you use this construction, you must order the counter variables correctly: from the inside For statement to the outside.

Common errors in For statements

These situations show some logic errors in writing For statements, and illustrate how LotusScript® responds to them.

  • Two For statements can be nested, but they cannot overlap partially.
    For i% = 1 To 3
       For j% = 1 To 2
    Next i%
       Next j%
    ' Output:
    ' Error 53: Name does not match FOR count variable: I
  • A For statement cannot overlap with any other block statement.
    For i% = 1 To 3
          Print "test"
    ' Output:
    ' Error 1: Unexpected: NEXT; Expected: LOOP
  • Within a For statement, its counter variable cannot be used as the counter variable of another For statement.
    For i% = 1 To 3
       For i% = 1 To 3
    ' Output:
    ' Error 52: FOR count variable already in use: I