If...Then...Else Statement |
No
If condition Then [statements] [ElseIf condition-n Then [elseifstatements] ... [Else [elsestatements]] End If
Or, you can use the single-line syntax:
If condition Then [statements] [Else elsestatements]
condition
Use: Required
Data Type: Boolean
An expression returning either True or False or an object type.
statements
Use: Optional
Program code to be executed if condition is True.
condition-n
Use: Optional
Same as condition.
elseifstatements
Use: Optional
Program code to be executed if the corresponding condition-n is True.
elsestatements
Use: Optional
Program code to be executed if the corresponding condition or condition-n is False.
Executes a statement or block of statements based on the Boolean (True or False) value of an expression.
If condition is True, the statements following the If are executed.
If condition is False and no Else or ElseIf statement is present, execution continues with the corresponding End If statement. If condition is False and ElseIf statements are present, the condition of the next ElseIf is tested. If condition is False, and an Else is present, the statements following the Else are executed.
In the block form, each If statement must have a corresponding End If statement. ElseIf statements don't have their own End If. For example:
If condition Then statements ElseIf condition Then statements End If
ElseIf and Else are optional, and any number of ElseIf and Else statements can appear in the block form. However, no ElseIf statements can appear after an Else.
condition can be any statement that evaluates to True or False.
If condition returns Null, it's treated as False.
You can also use the If statement to determine object types by using the TypeOf and Is keywords, as follows:
If TypeOf objectname Is objecttype Then
statements are optional only in the block form of If. However, statements are required when using the single-line form of If in which there is no Else clause.
You can use the single-line form of the If statement to execute multiple statements, which you can specify by delimiting the statements using colons; however, single-line form If statements are hard to read and maintain, and should be avoided for all but the simplest of situations.
In situations where you have many possible values to test, you will find the Select Case statement much more flexible, manageable, and readable than a bunch of nested If statements.
You will come across situations in which very large blocks of code have to execute based one or more conditions. In these—and in all situations—you should try to make your code as readable as possible, not only for other programmers, but for yourself when you try to maintain the code several months down the line. Take a common scenario in which, at the beginning of a procedure, a check is made to see if the procedure should in fact be executed under the current circumstances. You have the choice of surrounding the whole code with an If...Then...End If construct, like this:
If iSuccess Then ... ... ... 'x000 lines of code End If
Or you can switch the result to look for a False, then exit the sub, like this:
If Not iSuccess Then Exit Sub End If .... 'x000 lines of code
The difference is that, with the second method, you don't have to scroll down screens worth of code looking for the matching End If.
Indentation is important for the readability of If, and especially nested If, statements. The recommended indentation is four characters, which you will find is the Visual Basic editor's default tab value. The set of statements within each new If...Else..EndIf block should be indented. And here's a quick tip when using the VB5 or VBA development environment: you can select a block of code and press the Tab key to indent the complete selected block. The following example shows correctly indented code:
If x = y Then DoSomethingHere If y < z Then DoSomethingElseToo Else DoAnotherThing If z - 1 = 100 Then DoAThing End If End If Else DoAlternative End If
Use of the If statement requires some understanding of the implicit and explicit use of True in Visual Basic. The following If statement uses an implicit True:
If iSuccess Then
Notice that you are allowing VB to evaluate the iSuccess variable to True or False. When this implicit form is used, any non-zero value evaluates to True, and conversely a zero value evaluates to False. For example, the following code evaluates iSuccess as True and prints the "OK" message box:
Dim iSuccess As Integer iSuccess = 41 If iSuccess Then MsgBox "OK" Else MsgBox "False" End If
However, when you compare a variable to an explicit True or False, the value must be –1 to evaluate to True, and for False. If you amend the above code fragment as follows:
iSuccess = 41 If iSuccess = True Then
iSuccess doesn't evaluate to VB's version of True (-1). As you can imagine, this can lead to some confusion, since a variable can evaluate to True when using an implicit comparison but not when using an explicit comparison. Actually, just to add to the confusion, you could get the explicit comparison to behave the same as the implicit one by converting iSuccess to a Boolean:
If CBool(iSuccess) = True Then
This isn't entirely recommended, but it does show that VB's built-in constants of True and False evaluate only –1 and 0, respectively.
Logical comparison operators can be included in the condition expression, allowing you to make decisions based on the outcome of more than one individual element. The most common of these are And and Or. You can create conditions like:
If x = 1 And y = 3 Then
Visual Basic always evaluates both sides of a logical comparison, unlike some languages that allow you to stop before each portion of a comparison has been evaluated. For example, in the following code, if x does equal 1, then the If condition has been met. Some languages would stop the evaluation here. But regardless of the value of x, Visual Basic still evaluates the comparison with y.
If x = 1 Or y = 3 Then
The If statement is also used with objects to determine if an object has been successfully assigned to an object variable. (Actually, that's not completely accurate; you check to see if the object variable is still set to Nothing.) However, you can't use the equality operator (=) for this comparison. Instead, you must use the object comparison operator Is, as the following code shows:
If Not objectname Is Nothing Then