How to: Run Statements Depending on One or More Conditions
You can use an If...Then...Else Statement (Visual Basic) to run a specific statement or block of statements depending on the Boolean Data Type (Visual Basic) 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.
To run one or more statements if a condition is True
If you have only one statement to run, use the single-line syntax of the If...Then...Else construction. You do not need the Else or End If statements. The following example illustrates this.
Sub fixDate() Dim myDate As Date = #2/13/1973# If myDate < Now Then myDate = Now End Sub
-or-
To execute more than one line of code when the condition is True, use the 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. The following example illustrates this.
Dim alertLabel As New System.Windows.Forms.Label Sub alertUser(ByVal value As Long) If value = 0 Then alertLabel.ForeColor = System.Drawing.Color.Red alertLabel.Font = New Font(alertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) End If End Sub
To run some statements if a condition is True and others if it is False
Use an If...Then...Else construction with the Else (Visual Basic) statement to define two blocks of statements. Visual Basic runs one block if the condition is True and the other if it is False. The following example illustrates this.
Dim alertLabel As New System.Windows.Forms.Label Sub alertUser(ByVal value As Long) If value = 0 Then alertLabel.ForeColor = System.Drawing.Color.Red alertLabel.Font = New Font(alertLabel.Font, _ FontStyle.Bold Or FontStyle.Italic) Else alertLabel.Forecolor = System.Drawing.Color.Black alertLabel.Font = New Font(alertLabel.Font, _ FontStyle.Regular) End If End Sub
To test additional conditions if the first condition is False
Use an If...Then...Else construction with one or more ElseIf (Visual Basic) statements to test additional conditions if the first condition is False. In the following example, the Function procedure computes a payroll bonus based on performance rating. The statement block following the Else statement runs only if the conditions in the If and ElseIf statements are all 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. When it encounters a True condition or an Else statement, it runs the corresponding statement block. Control then passes to the statement following the End If statement.
You can have any number of ElseIf statements, or none at all. You can include or omit one Else statement regardless of whether you have any ElseIf statements.
See Also
Tasks
How to: Transfer Control Out of a Control Structure
How to: Test for Several Values of an Expression
How to: Retain Control When an Error Occurs
Concepts
Decision Structures
Loop Structures
Other Control Structures
Nested Control Structures