Conditionally executes a sequence of statements as a result of a Boolean expression.
If condition [ Then ] [ statements ] [ Else If elseif_condition [ Then ] [ elseif_statements ] ] ... [ Else [ else_statements ] ] End [ If ]
If condition Then statement [ Else else_statement ]
- Necessary Boolean expression
- Optional in the block construct
- Necessary in the one-line construct
- Optional in the block construct – One or more statements, executed only when condition is true..
- Necessary in the one-line construct – One statement
- Necessary after Else If – A Boolean expression calculated only when the condition or elseif_condition before this is false.
- Optional – One or more statements, executed only when an elseif_condition is true.
- Optional – One or more statements, executed only after the condition and any and all elseif_condition expressions are false
- Completes the block construct. You can also use End If.
When an If...Then...Else statement becomes too long, you can replace it with a Select...Case Statement.
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:
- @Option If With Then and @Option If Without Then
- @Option If With End
- @Option End With Block and @Option End Without Block
See @Option Directive for more information.
If Then End If If Then Else End If If Then Else If Then Else If Then Else End If If Then Else