Selecting one or the other with the If...Then...Else statement

The block statement If...Then...Else specifies conditional execution of either one group or another group of statements, depending on the value of an expression. Each statement group is usually just one short statement, since the entire If...Then...Else statement must be written on one line.

The syntaxes are:

If condition Then statements Else statements

In this form, either the Then clause is executed (if condition is TRUE); or the Else clause is executed (if condition is FALSE). For example:

If doCount% >= 1000 Then flagForm% = -1 Else flagForm% = 0

If condition Then statements

In this form, the Then clause is executed if condition is TRUE; otherwise, nothing is executed. For example:

If doCount% >= 1000 Then flagForm% = -1

For either form, execution continues with the statement on the next line. Nothing can follow the If...Then...Else statement on the same line, since LotusScript® recognizes a newline as the If...Then...Else statement terminator.

Note: Newline does not mean either chr(10) or chr(13) on all platforms. Newline is the character or sequence of characters that is used to mark the end of a line. This may be chr(10), or chr(13), but it may also be something else, because the actual value of newline depends on the platform.

This example shows a Then clause consisting of the single statement Exit Do. There is no Else clause. The script computes the elapsed time to execute 1000 iterations of a simple Do loop. Time may vary, depending on the workstation.

Dim doCount As Integer, startTime As Single
startTime! = Timer()
doCount% = 0
Do
   ' Increment doCount% through 1000 iterations of the Do loop.
   doCount% = doCount% + 1
   If doCount% > 1000 Then Exit Do
Loop
' Come here upon exit from the Do loop.
Print Timer() - startTime! "seconds for 1000 iterations"
' Output:
' .109375 seconds for 1000 iterations

For more information about the Do and Exit statements, see the sections on these statements in this chapter.

To include more than one statement in the Then clause, separate the statements by the colon (:) statement separator.

Do
   If doCount% >= 1000 Then Print "Done." : Exit Do 
Loop

You can rewrite the two statements in the Do loop in the preceding example as a single If...Then...Else statement.

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

This is a more compact loop than the one in the preceding example, but it runs more slowly.

The condition in the If...Then...Else statement can be simple, as in the preceding example, or complex.

This example shows a more complex condition:

If Abs(tempProx! - approx!) >= .00001 And iters% < 40 _
   Then Exit Do

LotusScript® identifies a statement as an If...Then...Else statement provided it has the form If condition Then, or If condition Then statements Else, followed on the same line by more source code. Unless this language appears on the same line, LotusScript® interprets the statement as an If...Then...ElseIf statement.

You can extend the statement to more than one line, by ending each line except the last with the line-continuation character, an underscore ( _ ). But if the statement is long enough to force continuation onto a second line, it may be more readable to rewrite it as an If...Then...ElseIf statement.