If..Then...Else...End If Statements

A series of statements that conditionally execute a block of embedded statements based upon the value of an expression.

If condition Then
    
[statements]
[ ElseIf elseif_condition Then
     [elseif_statements]]
         :
[ ElseIf elseif_condition Then
     [elseif_statements]]
[ Else
     [else_statements]]
End If

-or-

If condition Thenstatement

Prerequisites

None

Parameters

condition

Required expression that is interpreted as a True or False value. Any expression that yields a numeric result can be specified, not just Boolean expressions. Any expression that evaluates to <>0 is interpreted as a True condition.

statements

Optional statement or list of statements that are executed if the condition evaluates to True.

elseif_condition

Expression that is required if an optional ElseIf clause is specified. Any expression that yields a numeric result can be specified, not just Boolean expressions. Any expression that evaluates to <>0 is interpreted as a True condition.

elseif_statements

Optional statement or list of statements that are executed if the associated elseif_condition evaluates to True.

else_statements

Optional statement or list of statements that are executed if the Else clause is present and the precedingcondition and elseif_condition values all test False.

Remarks

This control structure tests one or more expressions and conditionally executes at most one block of statements or a single statement. It can be used to implement simple “either-or” types logic or more complex decisions based upon multiple conditions with multiple possible outcomes.

The If…Then statement begins by first testing the value of the condition. If the condition is True, the statements are executed, after which, all of the following program instructions are skipped until the closing End If is encountered. If the condition is False, the statements are skipped and processing continues at the first ElseIf, Else, or End If clause that follows the statements. Any condition that evaluates to <>0 will be interpreted as a True value.

An arbitrary number of ElseIf clauses can optionally follow the statements and precede the Else. If the condition is False, the first ElseIf clause is processed by evaluating its elseif_condition. If its elseif_condition is True, its elseif_statements are executed after which all of the following program instructions are skipped until the closing End If is encountered. If its elseif_condition is False, its elseif_statements are skipped and processing continues at the next ElseIf, Else, or End If clause that follows the elseif_statements.

An If…Then group of statements can contain a single optional Else statement. If the condition and all optional elseif_conditions have tested false, the optional else_statements will be executed.

For more complex logic, multiple If…Then…End If statements can be nested to an arbitrary depth and can be combined with other nested control structures. For example, a For loop can contain an If…Then…End If sequence which can in turn contain another If…Then…End If sequence.

Examples

Dim a As Boolean, b As Integer, c As Single
a = True
b = 20
If a AND (b > 10) Then ' This condition evaluates to True
c = 3.14159 ' This assignment will be executed
Else
c = 0 ' This assignment will be skipped
End If

See Also

Statements | Do… Loop Statements | For…Next Statements | GoTo Statements | Select...Case Statements | While…End While Statements