You can use If...Then...Else statements to execute a specific statement or block of statements depending on the Boolean value of a condition. The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (True or False). This includes values of other data types, such as numeric types, that have been converted to Boolean.
Executing Statements if a Condition Is True
To execute only one statement when a condition is True, you can use the single-line syntax of If...Then...Else, omitting the Else and End If statements, as in the following example:
Sub FixDate() Dim MyDate As Date = #2/13/1973# If MyDate < Now Then MyDate = Now End Sub
To execute more than one line of code when the condition is True, you use multiple-line syntax, which includes the End If statement. If you have no code to run when the condition is False, you omit the Else statement, as the following example shows:
Dim AlertLabel As New System.Windows.Forms.Label ' ... Sub AlertUser(ByVal Value As Long) If Value = 0 Then AlertLabel.ForeColor = Color.Red AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) End If End Sub
Executing Some Statements if a Condition Is True and Others if It Is False
You can use If...Then...Else with the Else statement to define two blocks of executable statements. One block is executed if the condition is True, the other if it is False.
Dim AlertLabel As New System.Windows.Forms.Label ' ... Sub AlertUser(ByVal Value As Long) If Value = 0 Then AlertLabel.ForeColor = Color.Red AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) Else AlertLabel.Forecolor = Color.Black AlertLabel.Font = New Font(AlertLabel.Font, _ FontStyle.Regular) End If End Sub
Testing Additional Conditions if the First Condition Is False
You can add one or more ElseIf statements to If...Then...Else to test additional conditions if the first condition is False. For example, the following Function procedure computes a payroll bonus based on performance rating. The statement following the Else statement runs only if the conditions in all of the If and ElseIf statements are False.
Function Bonus(ByVal Performance As Integer, ByVal Salary As Decimal) _ As Decimal If Performance = 1 Then Return Salary * 0.1 ElseIf Performance = 2 Then Return Salary * 0.09 ElseIf Performance = 3 Then Return Salary * 0.07 Else Return 0 End If End Function
Visual Basic tests the conditions in the order they appear in the If...Then...Else statements tests until it encounters a True condition or an Else statement, at which point it executes the corresponding statement block. Execution then branches to the end of the If...Then...Else block.
You can have any number of ElseIf statements, or none at all. You can include or omit an Else statement regardless of whether you have ElseIf statements.
If...Then...Else statements can be nested to as many levels as you need. However, for readability, you might want to use Select...Case statements rather than multiple levels of nested If...Then...Else statements or many ElseIf statements.