Defining Object Reference Variables
Working with Iterative Loops
LotusScript offers numerous iterative looping constructs including the DO, FOR, and WHILE statements. Each of these statements continuously loops through one or more executable statements based on an expression, value, or computed result.
There are two variations of the DO statement. This statement be can set up to loop either WHILE an expression is true or UNTIL an expression equates to true. In both cases, the condition can be evaluated at the start or end of the loop. The syntax is as follows:
DO [WHILE | UNTIL] condition action(s) LOOP
A.6.1 |
Alternatively, you can check the expression at the end of the loop by utilizing the following syntax:
DO action(s) LOOP [WHILE | UNTIL] condition
For example, the following code initializes a variable called counter to zero. Next, the LotusScript tests the condition to see if counter is less than five. For each loop, counter is incremented and a message is displayed to the user. The loop terminates when counter reaches the value of five.
Dim counter As Integer counter = 0 Do While counter < 5 counter = counter + 1 Messagebox counter Loop
The FOR statement is used to loop a specific number of times through a series of LotusScript statements. With this loop statement, a variable is set and incremented. The loop continues to execute until the desired number of iterations is reached. Optionally, you can specify an incremental value. If the STEP value is omitted, it defaults to an incremental value of one.
For example, an incremental step value of two means the loop will count by twos. The syntax for this statement is as follows:
FOR counter = startnum TO endnum [STEP increment] action(s) NEXT counter
For example, the following displays the counter number to the user. This counter value starts at one and loops five times. When the counter value equals six, the looping stops.
For counter = 1 To 5 Messagebox counter Next
Finally, the WHILE statement loops continuously, provided that the specified condition equates to a true value. When the condition returns a false value, LotusScript immediately exits the loop.
WHILE condition action(s) WEND
For example, this code asks the user if they would like to continue. If the user specifies either "Y" or "y", then the input box is redisplayed. The looping stops if any other value is returned.
Dim result As String result = Inputbox$("Do you want to continue (Y / N)?") While result = "Y" Or result = "y" result = Inputbox$("Do you want to continue (Y / N)?") Wend
Note
Best practices in software programming suggest that you should always create iterative loops that exit by default. This will prevent the possibility of an endless loop. An endless loop has the potential to lock up the application and client workstation and possibly to consume a significant amount of memory or CPU on the server. The EXIT statement can be used to conditionally exit an iterative loop. The following shows the syntax for this statement:
EXIT LoopType
where LoopType represents the current iterative loop such as DO, FOR, or FORALL. This statement can also be used to exit from a FUNCTION, SUB, or PROPERTY.
Communicating with Users
|