A variation of the If. . .Then statement is the If. . .Then. . .Else statement, which executes one block of statements if the condition is True and another block of statements if the condition is False. The syntax of the If. . .Then. . .Else statement is as follows:
If condition Then
statementblock1
Else
statementblock2
End If
Code language: VB.NET (vbnet)
Visual Basic evaluates the condition; if it’s True, VB executes the first block of statements and then jumps to the statement following the End If statement. If the condition is False, Visual Basic ignores the first block of statements and executes the block following the Else keyword. A third variation of the If. . .Then. . .Else statement uses several conditions, with the ElseIf keyword:
If condition1 Then
statementblock1
ElseIf condition2 Then
statementblock2
ElseIf condition3 Then
statementblock3
Else
statementblock4
End If
Code language: VB.NET (vbnet)
You can have any number of ElseIf clauses. The conditions are evaluated from the top, and if one of them is True, the corresponding block of statements is executed. The Else clause, which is optional, will be executed if none of the previous expressions is True. Listing 3.1 is an example of an If statement with ElseIf clauses.
Listing 3.1: Multiple ElseIf Statements
score = InputBox("Enter score")
If score < 50 Then
Result = "Failed"
ElseIf score < 75 Then
Result = "Pass"
ElseIf score < 90 Then
Result = "Very Good"
Else
Result = "Excellent"
End If
MsgBox Result
Code language: VB.NET (vbnet)
The order of the comparisons is vital when you’re using multiple ElseIf statements. Had you written the previous code segment with the first two conditions switched, like the following segment, the results would be quite unexpected:
If score < 75 Then
Result = "Pass"
ElseIf score < 50 Then
Result = "Failed"
ElseIf score < 90 Then
Result = "Very Good"
Else
Result = "Excellent"
End If
Code language: VB.NET (vbnet)
Let’s assume that score is 49. The code would compare the score variable to the value 75. Because 49 is less than 75, it would assign the value Pass to the variable Result, and then it would skip the remaining clauses. Thus, a student who scored 49 would have passed the test! So be extremely careful and test your code thoroughly if it uses multiple ElseIf clauses. You must either make sure they’re listed in the proper order or use upper and lower limits, as in the preceding sidebar.
The Abs method of the Math class returns the absolute value of a numeric value, and the string argument of the ToString method determines that the amount should have two decimal digits.
Multiple If. . .Then Structures versus ElseIf
Notice that after a True condition is found, Visual Basic executes the associated statements and skips the remaining clauses. It continues executing the program with the statement immediately after End If. All following ElseIf clauses are skipped, and the code runs a bit faster. That’s why you should prefer the complicated structure with the ElseIf statements used in Listing 3.1 to this equivalent series of simple If statements:
If score < 50 Then
Result = "Failed"
End If
If score < 75 And score >= 50 Then
Result = "Pass"
End If
If score < 90 And score > =75 Then
Result = "Very Good"
End If
If score >= 90 Then
Result = "Excellent"
End If
Code language: VB.NET (vbnet)
With the multiple If statements, the compiler will generate code that evaluates all the conditions, even if the score is less than 50.
The IIf() Function
Not to be confused with the If. . .Then statement, VB provides the IIf() function. This built-in function accepts as an argument an expression and two values, evaluates the expression, and returns the first value if the expression is True, or the second value if the expression is False. The syntax of the IIf() function is the following:
IIf(expression, TruePart, FalsePart)
Code language: VB.NET (vbnet)
The TruePart and FalsePart arguments are objects. (They can be integers, strings, or any built-in or custom object.) The IIf() function is a more compact notation for simple If statements. Let’s say you want to display one of the strings ‘‘Close” or ‘‘Far”, depending on the value of the distance variable. Instead of a multiline If statement, you can call the IIf() function as follows:
IIf(distance > 1000, "Far", "Close")
Code language: VB.NET (vbnet)
Another typical example of the IIf() function is in formatting negative values. It’s fairly common in business applications to display negative amounts in parentheses. Use the IIf() statement to write a short expression that formats negative and positive amounts differently, like the following one:
IIf(amount < 0, "(" &
Math.Abs(amount).ToString("#,###.00") & ")",
amount.ToString("#,###.00"))
Code language: VB.NET (vbnet)