Advanced Conditional Expressions
- Multiple expression evaluation
- Conditional blocks are used to evaluate the validity of an expression and act upon it accordingly; however, If statements are not limited to single expression evaluation. The keywords And, Or, and Xor can be implemented to evaluate two or more related expressions within the same statement.
- And
- The keyword And is used to verify that two or more expressions are true. If all of the expressions are true, then the corresponding instructions will be performed; otherwise, the If statement will be invalidated. The following is an example of And being used to verify that the length of a new STATISTICA Report is twelve and that it is visible; if either of these statements are false then the Report will be forced to be visible:
Sub main
'create a report,write to it 'and make it visible Dim r As New Report r.Visible = True r.SelectionText = "Report text." 'if it is visible and it contains 12 characters 'then display a messagebox. If either of these 'expressions are false then the messagebox will 'not appear If r.Visible And r.CharacterCount = 12 Then
MsgBox "The report is visible and" & _ vbCrLf & "has twelve characters."
Else
r.Visible = True
End If
End Sub
- Or
- The keyword Or is used to verify that at least one expression within an If statement is true. If any of the expressions are true then the corresponding instructions will be performed; otherwise, the If statement will be invalidated. The following example demonstrates a report being verified as to whether it is visible or contains twelve characters. If either is true then a message box will be displayed verifying that; otherwise, an error message will be displayed:
Sub main
'create a report and write to it ',but it will be left invisible Dim r As New Report r.Visible = False r.SelectionText = "Report text." 'if it is visible or it contains 12 characters 'then display a messagebox. If both of these 'expressions are false then an 'error messagebox will appear If r.Visible Or r.CharacterCount = 12 Then MsgBox "The report either visible or" & _ vbCrLf & "has twelve characters." Else MsgBox "The report is neither visible nor" & _ vbCrLf & "does it have twelve characters." End If
End Sub
- Xor
- The keyword Xor is used to verify that one, and only one, expression in a statement is true. If no expressions are true, or if two or more expressions are true, then the entire If statement will be invalidated. The following example demonstrates retrieving STATISTICA's window state and displaying a message if only one of the three possibilities is true:
Sub main
Dim WS As WindowState WS = Application.WindowState If WS = scNormal Xor WS = scMaximized _
Xor WS = scMinimized Then
MsgBox _ "STATISTICA is either minimized," _ & vbCrLf &"Normal, or maximized"
Else
MsgBox "STATISTICA is in an invalid window state"
End If
End Sub
The reason that Xor should be used in a situation such as this is because is verifies that only one of the window states is currently valid. It wouldn't be logical for STATISTICA to be two different window states at the same time, and Xor is able to validate that.
- Nested conditional blocks
- Conditional block can also be inserted inside of other conditional block; however, the outer conditional block must be true for the nested (or embedded) conditional block to even be evaluated. The following example illustrates this:
Sub main
'create a new spreadsheet and make it invisible Dim s As New Spreadsheet s.Visible = False 'if it's visible then... If s.Visible Then
'because it's invisible, then the following 'will not be evaluated (whether or not it 'is true) If s.Cases.Count = 10 Then
MsgBox "The spreadsheet has 10 cases"
'This End If will close the closest If statement, 'which happens to be the one verifying whether 'the case count is ten or not End If
'Because the previous (nested) If statement has 'been closed, then this else will refer to the 'next closest If statement which is still open, 'which happens to be one verifying whether or 'not the spreadsheet is visible or not Else
MsgBox "The spreadsheet is invisible"
'This End If will close the next closest If 'statement that is still open, which happens 'to be the one verifying whether or 'not the spreadsheet is visible or not End If
End Sub