Domino Code Fragment

Code Name*
Conditional Branching: The WHILE Statement
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.3.133.108.241
Description*
While loops execute code conditionally.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
While loops execute code conditionally. Statements between the While statement and the Wend statement is executed repeatedly until a condition is no longer true. For instance, a program could ask a user for inventory items repeatedly until the user enters a blank item.

Note: If a While statement's condition can never be met, the result will be an `endless loop', meaning the statements in the loop will execute over and over until you stop the program.

Syntax
While condition
statements
Wend

The following example displays a messagebox with the value of X in it nine times. In the tenth iteration, X is not less than 10, it is equal to 10, so the statements in the loop are passed by, and the message `Finished!' is displayed. Notice that if the line X = X + 1 were removed, that you would have an endless loop. X would never change from a value of 1, so the condition X < 10 would never fail.

Click here to view the example.

Dim X As Integer 'Declare X as type Integer
X = 1 'Assign X a numeric value of 1
While X < 10 'While X is less than 10, execute the following statements
Messagebox X 'Display the value in X
X = X + 1 'Add one to the value in X
Wend 'End of WHILE loop
MessageBox "Finished!" 'Tell the user the code is finished

The next example is an improved way to check to see if a value from an inputbox is numeric. Using an IF loop, (As in the example located in the document Conditional Branching: The IF Statement) an error message is displayed if the value is not numeric. With the While loop, we can ask for the value repeatedly until a numeric value is entered.

Click here to view the example.

Dim X As Variant 'Delcare X as type Variant
X = Inputbox("What is your age?")
'Ask for the user's age
While Not Isnumeric(X)
'While X is not a numeric value
Messagebox "You did not enter a number!"
'A number was not entered
X = Inputbox("What is your age?")
'Ask for the age again
Wend
'End of WHILE loop
Messagebox "You are " + Cstr(X) + " years old!"
'The age entered