Controlling Program Execution

Using conditional statements, you can write BSL code that makes decisions and repeats actions. The following conditional statements are available in BSL:

  • If...Then...Else statement
  • Select Case statement

Note:

The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually, the condition is an expression that uses a comparison operator to compare one value or variable with another. For information about comparison operators, see Comparison Operators. If...Then...Else statements can be nested to as many levels as you need.

Running Statements if a Condition is True

To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. The following example shows the single-line syntax.

This example omits the Else keyword.

Sub Test()
    Dim myNumber
    myNumber = 10
    If myNumber < 15 Then myNumber = 15
    'myNumber: 15
End Sub

To run more than one line of code, you must use the multiple-line (or block) syntax. This syntax includes the End If statement, as shown in the following example:

Sub Test()
    Dim myNumber
    myNumber = 10
    If myNumber < 15 Then 
        myNumber = 15
    End If
    'myNumber: 15
End Sub

Running Certain Statements if a Condition is True and Running Others if a Condition is False

You can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.

Sub Test(value)
    If value = 0 Then
        value = 10
    Else
        value = 20
    End If
    ' Outputs: value: 10 if value was 0, otherwise 20
End Sub

Deciding Between Several Alternatives

A variation on the If...Then...Else statement allows you to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so you can control program flow based on different possibilities. For example:

Sub ReportValue(value)
If value = 0 Then
        	    value = 10
    	ElseIf value = 1 Then
        	    value = 20
    	ElseIf value = 2 Then
        	    value = 30
    	Else
        	    value = 40
    	End If
    	'value: 20
End Sub
ReportValue(1) ' Condition ElseIf value = 1 is met and value will be set to 20

You can add as many ElseIf clauses as you need to provide alternative choices. Extensive use of the ElseIf clauses often becomes cumbersome. A better way to choose between several alternatives is the Select Case statement.

Sub NestedIf(value)
    If value < 10 Then
        If value < 5 Then
            'value is less than 5
        Else
            'value is between 5 and 9
        End If
    Else
        'value is 10 or greater
    End If
End Sub

NestedIf(6)   ' Triggers the logic: value is between 5 and 9

Making Decisions with Select Case

The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability like If...Then...Else statement, but it makes code more efficient and readable.

A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed, as in the following example.

The following examples illustrates the use of the select case:

Example 1:

Dim age
age = (30/6) * 6   ' age = 30
Select Case True
    Case age < 18
        'You're a minor Section
    Case age >= 18 And age <= 65
        'You're an adult Section
    Case Else
        'You're a senior citizen Section
End Select

Example 2:

Dim grade
grade = "B"
Select Case grade
	Case "A", "B", "C"
		'Pass Section
	Case "D"
		'Barely Passed Section
	Case "F"
		'Fail Section
	Case Else
		'Invalid grade Section
End Select

Note:

The Select Case structure evaluates an expression once at the top of the structure. In contrast, the If...Then...ElseIf structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...ElseIf structure with a Select Case structure only if each ElseIf statement evaluates the same expression.