CitectVBA Programming Reference > Understanding CitectVBA Language Basics > Control Structures > If statement

If statement

The If statement in CitectVBA tests an initial condition and then either performs or omits to perform the statements it contains, dependant upon the logical result of the test condition. The condition can be a comparison or an expression, and must logically evaluate to either True or False. The If statement has both single line and multiple line syntax structure.

The single line syntax uses the If <TestCondition> Then <StatementToPerformIfTrue> structure, however, can only perform a single statement if and only if the test condition result is True. No 'End If' statement is required:

If <Condition> Then <Statement>

If the result of the If test condition was True, the program flow continues into and performs the statement following the Then statement, until it reaches the end of the line.

To perform a single statement conditionally upon a False result, use the NOT logical operator:

If NOT <Condition> Then <Statement>

To perform multiple statements, use the multiple line syntax structure which ends with the 'End If' statement:

If <Condition> Then
' Then statement block
' perform only if true
<Statement/s>
End If

If the result of the If test condition was True, the program flow continues into the Then statement block, and performs the statements following the Then statement, until it reaches the End If statement.

If the result of the If test condition was False, the program flow jumps over the Then statement block, which in this case exits the If structure (without performing any statements other than the initial test condition).

The mutiple line If structure can perform different blocks of statements dependant upon EITHER a True OR a False result to the test condition, through the use of the Else statement block:

If <Condition> Then
' Then statement block
' perform only if true
<Statement/s>
Else
' Else statement block
' perform only if false
<Statement/s>
End If

If the result of the If test condition was True, the program flow performs the Then block statements, until it reaches the Else statement. It then jumps over the Else statement block and exits the If structure (without performing any of the Else statement block statements).

If the result of the If test condition was False, the program flow jumps over the Then statement block (without performing any of those statements) to the Else statement to perform the statements in the Else statement block until it reaches the End If statement.

Further test conditions can be placed into an If structure through the use of the optional Else If <Condition> statement block. ElseIf statement blocks can only be positioned within an If structure before the Else statement block.

If <Condition> Then
' Then statement block
' perform only if true
<Statement/s>
ElseIf <Condition>
' Else If statement block
' perform only if true
<Statement/s>
Else
' Else statement block
' perform only if false
<Statement/s>
End If

The ElseIf test condition is only evaluated after the initial If structure test condition results in False.

If the result of the ElseIf test condition was True, the statements within the ElseIf statement block are performed. The program flow then jumps over the Else statement block and exits the If structure (without performing any of the Else statement block statements).

If the result of the ElseIf test condition was False, the program flow jumps over the ElseIf statement block (without performing any of those statements) to the Else statement to perform the statements in the Else statement block until it reaches the End If statement.

There is no apparent limit to the number of Else If statement blocks that any one If structure can hold, however, the Select Case Statement structure handles multiple condition result alternatives much more efficiently.

See Also

Control Structures