LOTUSSCRIPT LANGUAGE


Do and Do...While loops
The iterative block statement Do executes a block of statements repeatedly while a given condition is true, or until it becomes true. The block of statements executes infinitely only if the condition for termination is never satisfied.

The three kinds of Do statements differ in whether there is a condition or in where the condition appears in the statement. There may be no condition at all, or it may be specified at the beginning, or at the end, using either a While phrase or an Until phrase.

The syntax is:


This example illustrates the first form of Do statement. The Do loop repeats until the condition in the If statement is satisfied. A Do statement like this one, without a While phrase or an Until phrase, must contain an Exit statement or an End statement, or some other statement that transfers control out of the Do statement, such as GoTo. Otherwise the loop runs forever.

doCount% = 0
Do
  doCount% = doCount% + 1
  If doCount% >= 1000 Then Exit Do
Loop

In this example, each Do statement is equivalent to the Do statement in the preceding example:

Dim doCount As Integer

' A Do While statement (condition at the beginning)
doCount% = 0
Do While doCount% < 1000
  doCount% = doCount% + 1
Loop

' A Do Until statement (condition at the beginning)
doCount% = 0
Do Until doCount% >= 1000
  doCount% = doCount% + 1
Loop

' A Do...Loop While statement (condition at the end)
doCount% = 0
Do
  doCount% = doCount% + 1
Loop While doCount% < 1000

' A Do...Loop Until statement (condition at the end)
doCount% = 0
Do
  doCount% = doCount% + 1
Loop Until doCount% > 1000

The forms of the Do statement differ with regard to whether the condition is tested before or after the first iteration of the loop. The condition in a Do While or a Do Until condition statement is tested before the first iteration, whereas the condition in a Do...Loop While or a Do...Loop Until condition statement is not tested until after the first iteration. As a result:


This example shows the difference:

Dim doCount As Integer

doCount% = 1
Do While doCount% < 1
  doCount% = doCount% + 1
Loop
Print "Do While...Loop counter reached" doCount%

doCount% = 1
Do
   doCount% = doCount% + 1
Loop While doCount% < 1
Print "Do...Loop While counter reached" doCount%
' Output:
' Do While...Loop counter reached  1
' Do...Loop While counter reached  2

The Do statement doesnft establish a separate scope for variables within it. A variable used in a While condition clause or an Until condition clause is like any other variable in the script. If the variable has not been used previously, then its appearance in condition declares it implicitly, and initializes it.

For example:

' Suppose that the variable named doCount%
' has not appeared in a script prior to its appearance here.
Do While doCount% < 1
  doCount% = doCount% + 1
Loop
Print "Do While...Loop counter reached" doCount%
' Output:
' Do While...Loop counter reached  1

LotusScript declares doCount% implicitly and initializes it to 0, so the body of the loop executes once. However, itfs risky programming practice to rely on this initialization. You couldnft rely on this behavior without knowing that either doCount% has not appeared earlier during execution, or that the current value of doCount% is 0.

In this example, a Do statement calculates successive terms of a sequence of numbers that converges to a limit:

' This sub computes the quotient of each successive pair of
' terms of the Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, ...
' The sequence of quotients 2, 3/2, 5/3, ... is known to
' converge to the golden mean (1 + Sqr(5))/2.
' The sub argument deltaLim! is the tolerance.
' This example illustrates the Do...Loop Until form of the
' Do statement, with a condition that is recomputed on each
' iteration.
Sub FibiLim (deltaLim As Single)
  Dim r1 As Single, r2 As Single, r3 As Single
  Dim limTrue As Single      
  Dim i As Integer
  ' Initialize the Fibonacci numbers and a counter.
  r1! = 1
  r2! = 1
  r3! = 1
  i% = 2
  Do
  NexTerm:
     i% = i% + 1
     r1! = r2!
     r2! = r3!
     ' r3! is the next Fibonacci number.
     r3! = r2! + r1!
     Print i%, "f(" & Str(i%) & "):" r3!, "quotient: " _
        r3!/ r2!
     ' On the first iteration, disable the standard exit
     ' condition.
     If i% = 3 GoTo NexTerm
     ' Iterate until successive quotients are close.
     ' The sequence is known to converge, so the iteration
     ' will end.
  Loop Until Abs(r3! / r2! - r2! / r1!) < deltaLim!
  limTrue! = (1 + Sqr (5)) / 2
  ' When done, show the closeness obtained and the actual
  ' limit.
  Print "Tolerance:" deltaLim!
  Print "Difference:" CSng(Abs(r3! / r2! - limTrue!)), _
     "(Actual limit:" limTrue!")"
End Sub
' Call FibiLim with a tolerance argument.
Call FibiLim(.005)
' Output:
' 3           f(3): 2       quotient:  2
' 4           f(4): 3       quotient:  1.5
' 5           f(5): 5       quotient:  1.66666666666667
' 6           f(6): 8       quotient:  1.6
' 7           f(7): 13      quotient:  1.625
' 8           f(8): 21      quotient:  1.61538461538462
' 9           f(9): 34      quotient:  1.61904761904762
' Tolerance: .005   Difference: 1.013614E-03  
' (Actual limit: 1.618034)

See Also