If…Else Statement

Executes a sequence of statements because of a condition.

Syntax

Block construct

If condition [ Then ]
    [ statements ]
[ Else If else_if_condition [ Then ]
    [ else_if_statements ] ]
…
[ Else
    [ else_statements ] ]
End [ If ]

One-line construct

If condition Then statement [ Else else_statement ]

Parts

condition
Mandatory Boolean expression.
Then
Optional in the block construct
Mandatory in the one-line construct
statement(s)
Execute only when condition is true..
Optional in the block construct, one or more statements.
Mandatory in the one-line construct, a one-line statement.
Else If
Optional keywords that you can use again and again.
else_if_condition
Mandatory after Else If, a Boolean expression calculated only when condition (or else_if_condition before this one) is false.
else_if_statements
Optional one or more statements, executed only when elseif_condition is true.
else_statement(s)
Optional one or more statements, executed only after condition and all of else_if_condition(s) are false.
End
Completes the block construct. You can also use End If.

Instructions

Block construct

TODO

When If…Else becomes too long, you can replace it with a Select…Case Statement.

One-line construct

You can use the one-line construct for short, easy tests. There are a small number of statements that can be used with this construct. They include:

Changes in syntax with @Option

There are three areas where you can change the syntax of the statement:

See @Option Directive for more information.

Examples

If x = 1 Then
    Call First
End If

If x = 1 Then
    Call First
Else
    Call Other x
End If

If x = 1 Then
    Call First
Else If x = 2 Then
    Call Second
Else If x = 3 Then
    Call Third
Else
    Call Other x
End If

If x = 1 Then Call First

If x = 1 Then Call First Else Call Other x

See also