If...Then...Else Statement

Conditionally executes a sequence of statements as a result of a Boolean expression.

Syntax

Block construct

If condition [ Then ]
    [ statements ]
[ Else If elseif_condition [ Then ]
    [ elseif_statements ] ]
...
[ Else
    [ else_statements ] ]
End [ If ]

One-line construct

If condition Then statement [ Else else_statement ]

Parts

condition
Necessary Boolean expression
Then
Optional in the block construct
Necessary in the one-line construct
statement(s)
Optional in the block construct – One or more statements, executed only when condition is true..
Necessary in the one-line construct – One statement
elseif_condition
Necessary after Else If – A Boolean expression calculated only when the condition or elseif_condition before this is false.
elseif_statements
Optional – One or more statements, executed only when an elseif_condition is true.
else_statement(s)
Optional – One or more statements, executed only after the condition and any and all elseif_condition expressions are false
End
Completes the block construct. You can also use End If.

Instructions

Block construct

When an If...Then...Else statement 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 not many statements that can be used with this construct. They include assignment, method call, Call, Call Event, Exit, GoTo Case, ReDim, Return, and Throw.

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 Then
End If

If Then
Else
End If

If Then
Else If Then
Else If Then
Else
End If

If Then Else

See also