Working with Conditional Branching

Conditional statements are used to execute one or more statements based on the result of an expression. The two most commonly used branching statements include the IF and SELECT CASE statements.

The IF statement is used to conditionally execute code based on an expression. If the resulting expression equates to true, then the action or actions are executed. Alternatively, if the resulting expression equates to false, then a secondary set of instructions is executed (when specified using the keyword ELSE). It's important to note that the ELSE clause is optional. There are two formats for the IF statementsingle line and multiple line. The format for the single-line IF statement is as follows:

IF expression THEN action1 ELSE action2

Alternatively, the multiple-line IF statement is used to execute a block of statements depending on the expression result. All multi-line statements must start with the keyword IF and end with the keyword END IF. Optionally, you can also include an ELSE statement with a secondary block of actions to be run. The following shows the syntax for a multi-line statement.

IF expression THEN action1a action1b action1c ... ELSE action2a action2b action2c ... END IF

In both cases, the ELSE statement is optional and can be omitted. This means that if the expression equates to false and the statement does not have an ELSE clause, then no statements will be executed. For example, if the value of the status variable equals "New", then the message "This is a new service request" is displayed. Otherwise, if the values do not equate, the alternate message is displayed. Finally, you can nest multiple IF statements by using the ELSEIF statement.

IF status = "New" THEN messagebox "This is a new service request" ELSE messagebox "This is an existing service request" END IF

Like the IF statement, the SELECT statement is used to execute one or more instructions based on an expression result. The following shows the syntax for this statement.

SELECT CASE variable CASE value1 : action(s) CASE value2 : action(s) CASE value3 : action(s) CASE ELSE action(s) END SELECT

You'll notice that with this statement, an expression or variable is defined at the start of the SELECT statement and subsequently compared with each CASE statement value. After the first match is identified, the action (or actions) for the specific case is executed, and LotusScript exits the SELECT statement. If no match is found, an optional set of actions can be specified in the ELSE clause.

For example, the following code asks the user how many items should be ordered and stores the response in the variant variable called result. This value is then compared with each case statement, and an appropriate message is displayed if a match is found. If no match is found, a warning message along with the user's response is displayed instead.

Dim result As String result = Inputbox$("Do you want 1, 2 or 3 items?") Select Case result Case "1" : Messagebox "One item ordered" Case "2" : Messagebox "Two items ordered" Case "3" : Messagebox "Three items ordered" Case Else Messagebox "Incorrect value: " + result End Select

Working with Iterative Loops

Категории