How Do I Specify a Conditional Instruction?

There are two different types of conditional evaluations in STATISTICA Visual Basic: the If...Then block and the Select Case block.

Utilizing an If...Then block.

The general syntax for an If...Then block is diagramed in the following structure:

If [Expression] Then

[Instruction]

ElseIf [Expression]Then

[Instruction]

Else

[Instruction]

End If

The components of the If...Then block are:

If...Then. This is the entry point for the conditional block. The corresponding expression will be evaluated, and if it is true then the respective instructions will be performed and the conditional block will be exited; however, if it is false then the conditional block will continue and the respective instructions will not be executed.

Expression. This is the statement or variable which will be evaluated to see whether it is true or false.

Instruction. These will be any valid STATISTICA Visual Basic events which execute if the corresponding expression is found to be true.

ElseIf...Then. This will be the next expression to be evaluated in the block. It will not be evaluated unless the first expression is found to be false. This is optional and is not necessary if only a single expression needs to be evaluated. Note, you must have a corresponding If to use ElseIf.

Else. This will be the final set of instructions to be executed if no expressions in the conditional block are found to be true. This is optional, but it is recommended for error handling and data validation. Note that an expression here is not necessary because it will simply execute if nothing else was true. Also, you must have a corresponding If to use Else.

End If. Unless there is only one If statement with one instruction on the same line, all If blocks must have a corresponding End If to terminate it.

The If...Then block is the most common and flexible way to evaluate a condition and execute instructions if certain conditions are met. The following example demonstrates the syntax of an If block:

Sub main

'create a report and make it visible
Dim
r As New Report

r.Visible = True

'evaluate whether or not it's visible

'if it is visible

If
r.Visible Then

MsgBox "The Report is visible"

'else if it is not visible
ElseIf
Not(r.Visible) Then

MsgBox "The Report is invisible"

'if nothing above was true then...
Else

MsgBox _
"The Report is neither visible or invisible"

'indicate that the If block is done
End If

End Sub

See Advanced conditional expressions for additional If...Then features.

Utilizing a Select Case block.

The general syntax for a Select Case block is diagramed in the following structure:

Select Case [Variable]

Case [FirstExpression]

[Instruction]

Case [SecondExpression]

[Instruction]

Case Else

[Instruction]

End Select

The components of the Select Case block are:

Select Case. This is the entry point for the conditional block. The corresponding variable will have its value evaluated and compared to each of the cases listed in the conditional block. If a case is found to be the same as the value of the variable, then its respective instructions will be executed and the conditional block will be exited; otherwise, the Case Else (if included) instructions will be executed.

Variable. This will be the variable which is examined and compared with all of the conditional block's cases.

Case FirstExpression. This will be any data value that the evaluated variable may represent. If they are found to be the same then the corresponding instructions will be performed and the conditional block will be exited; otherwise, the next Case in the conditional block will be compared with the variable.

Instruction. These will be any valid STATISTICA Visual Basic events which will take place if the corresponding Case is found to be the same as the variable's value.

Case Else. This will be the final set of instructions to be executed if no expressions in the conditional block are found to be true. This is optional, but it is recommended for error handling and data validation. Note that an expression here is not necessary because it will simply execute if no Case matched the variable.

End Select. This is the exit point for the Select Case block.

A Select Case is much easier to read and organize when compared to a rather lengthy If...Then block; however there are limitations: a Select Case can only evaluate a single variable and cannot evaluate expressions. A Select Case is designed to evaluate the value of a specific variable and act upon it, but it cannot evaluate true or false expressions (e.g., whether a spreadsheet is empty or if a variable is either greater or less than a certain value ). For these types of situations an If...Then block is recommended. The following is an example of a Select Case which evaluates the value of a variable called Number and outputs its value if found:

Sub main

Dim Number As Integer
Number = 3

'Evalute the value of the variable
Number

Select Case
Number

'Is the value of Number 1?
Case
1

MsgBox "Number is 1"

'Is the value of Number 2?
Case
2

MsgBox "Number is 2"

'Otherwise...
Case Else

MsgBox "Number isn't 1 or 2"

End Select

End Sub