Skip to content

Loops

Introduction

There are no additions to the available iterative statements (loops) in VoltScript. But the pre- and post-fix increment and decrement operators may change the way code is written.

Iterative statements

Like LotusScript, VoltScript provides the following iterative statements:

  • For...Next loop uses a numeric variable, iterating from a lower bound to an upper bound, both defined in the For statement. The numeric variable is automatically incremented by the loop.
  • ForAll...in loop iterates all elements in an array, list or VoltScript Extension class that implements a collection. Exit ForAll statement allows you to exit early.
  • While loop iterates according to a condition, checked at the start of each loop. There is no Exit statement provided to break out of this condition, so if you wish to do this, best practice is to use a Do... loop instead. It's the developer's responsibility to remember to change the condition variable within the loop.
  • Do...Loop loop iterates without condition until an Exit Do statement is triggered.
  • Do While...Loop loop checks a condition before starting each loop and, as long as the condition is true, executes the conditions. It's the developer's responsibility to remember to change the condition variable within the loop or call Exit Do.
  • Do...Loop While loop checks a condition after each loop, always iterating the loop at least once. The loop is iterated again as long as the condition is true. It's the developer's responsibility to remember to change the condition variable within the loop or call Exit Do.
  • Do Until...Loop loop checks a condition before starting each loop and executes the loop until the condition is true. It's the developer's responsibility to remember to change the condition variable within the loop or call Exit Do.
  • Do...Loop Until loop checks a condition after each loop, always iterating the loop at least once. The loop is iterated again until a condition is true. It's the developer's responsibility to remember to change the condition variable within the loop or call Exit Do.

This flow diagram should help you choose the right iterative loop:

Iterative loops

Success

Use of the right iterative statement should prevent error conditions or the need to use GoTo, improving readability and support of the code.

Prefix/Postfix increment/decrement

When using numeric variables in While and Do loops, historically a separate instruction to increment or decrement the variable was required. However, prefix and postfix increment and decrement operators can avoid the need for this.

Warning

You may need to think carefully about whether to use prefix or postfix operators. Remember with a prefix operator, the variable will be modified before any other instructions on the line are processed; with the post-fix operator, all other instructions on the line will be processed and then the variable will be modified.

While loop

The While loop is probably the most familiar to LotusScript developers. The following example will print all elements of an array, incrementing the variable after the print:

Sub loopArray(passedArr as Variant)
    Dim i as Integer
    While i <= UBound(passedArr)
        Print passedArr(i++)
    Wend
End Sub

Do with prefix or postfix increment

The danger of incrementing a variable within the loop is that the unobservant developer may wish to make an additional use of the array element, so copy and paste passedArr(i++), inadvertently double-incrementing the variable i within each loop. Do loops can avoid the temptation by ensuring the variable is incrementing in the loop command.

However, incrementing at the start of the loop will skip the first variable, so the condition must be at the end. This can be achieved with either prefix or postfix operator. The following code uses the prefix operator.

Sub loopArrayDo(passedArr as Variant)
    Dim i as Integer
    Do
        Print passedArr(i)
    Loop Until ++i > UBound(passedArr)
End Sub

The following code does the same with a postfix operator:

Sub loopArrayDoPostfix(passedArr as Variant)
    Dim i as Integer
    Do
        Print passedArr(i)
    Loop Until i++ = UBound(passedArr)
End Sub

More complex postfix operations

Imagine we want to iterate the words in "The quick brown fox jumps over the lazy dog" but exit at the second "the". Postfix operators can make the code very terse:

Sub loopArrayExitThe(passedArr as Variant)
    Dim i as Integer
    Dim the as Integer
    Do Until i > UBound(passedArr)
        If (LCase(passedArr(i)) = "the") Then
            If the++ > 0 Then Exit Do
        End If
        Print passedArr(i++)
    Loop
End Sub

Line 5 checks if the lower-cased word is "the". Line 6 checks a variable is greater than 0 and then increments it, allowing the conditional statement to be reduced to a single line. After printing the current word, the variable for the Do loop is incremented.

The complete implementations of the code snippets are available on GitHub.