## Comparison operators

There are several ways to discuss what is evaluated in an If statement. Essentially, the code between the If and Then portions of the statement must eventually evaluate out to a Boolean. At the most basic level, this means you can write If True Then, which results in a valid statement, although the code would always execute the associated block of code with that If statement. The idea, however, is that for a basic comparison, you take two values and place between them a comparison operator. Comparison operators include the following symbols: =, >, <, >=, <=.

Additionally, certain keywords can be used with a comparison operator. For example, the keyword Not can be used to indicate that the statement should consider the failure of a given comparison as a reason to execute the code encapsulated by its condition. An example of this is shown in the next example:

'Code A1 End If

It is therefore possible to compare two values and then take the resulting Boolean from this comparison and reevaluate the result. In this case, the result is only reversed, but the If statement supports more complex comparisons using statements such as And and Or. These statements enable you to create a complex condition based on several comparisons, as shown here:

If Not i = 1 Or i < 0 And str = "Hello" Then 'Code A1

Else

'Code B2 End If

The And and Or conditions are applied to determine whether the first comparison's results are true or false along with the second value's results. The And conditional means that both comparisons must evaluate to true in order for the If statement to execute the code in block A1, and the Or statement means that if the condition on either side is true, then the If statement can evaluate code block A1. However, in looking at this statement, your first reaction should be to pause and attempt to determine in exactly what order all of the associated comparisons occur.

There is a precedence. First, any numeric style comparisons are applied, followed by any unary operators such as Not. Finally, proceeding from left to right, each Boolean comparison of And and Or is applied. However, a much better way to write the preceding statement is to use parentheses to identify in what order you want these comparisons to occur. The first If statement in the following example illustrates the default order, while the second and third use parentheses to force a different priority on the evaluation of the conditions:

If ((Not i = 1) Or i < 0) And (str = "Hello") Then If (Not i = 1) Or (i < 0 And str = "Hello") Then If Not ((i = 1 Or i < 0) And str = "Hello") Then

All three of the preceding If statements are evaluating the same set of criteria, yet their results are potentially very different. It is always best practice to enclose complex conditionals within parentheses to indicate the desired order of evaluation. Of course, these comparisons have been rather simple; you could replace the variable value in the preceding examples with a function call that might include a call to a database. In such a situation, if the desired behavior were to execute this expensive call only when necessary, then you might want to use one of the shortcut comparison operators.

Since you know that for an And statement both sides of the If statement must be true, there are times when knowing that the first condition is false could save processing time; you would not bother executing the second condition. Similarly, if the comparison involves an Or statement, then once the first part of the condition is true, there is no reason to evaluate the second condition because you know that the net result is success. In this case, the AndAlso and OrElse statements allow for performance optimization:

If ((Not i = 1) Or i < 0) AndAlso (MyFunction() = "Success") Then If Not i = 1 OrElse (i < 0 And MyFunction() = "Success") Then

The preceding code illustrates that instead of using a variable like str as used in the preceding samples, your condition might call a function you've written that returns a value. In this case, MyFunction would return a string that would then be used in the comparison. In the statements above, each conditional statement has been optimized so that there are situations where the code associated with MyFunction won't be executed.

This is potentially important, not only from a performance standpoint, but also in a scenario where, given the first condition, your code might throw an error. For example, it's not uncommon to first determine whether a variable has been assigned a value and then to test that value. This introduces yet another pair of conditional elements: the Is and IsNot conditionals.

Using If enables you to determine whether a variable has been given a value, or to determine its type. In the past it was common to see nested If statements, with the first determining whether the value was null, followed by a separate If statement to determine whether the value was valid. Starting with .NET 2.0, the short-circuit conditionals enable you to check for a value and then check whether that value meets the desired criteria. The short-circuit operator prevents the check for a value from occurring and causing an error if the variable is undefined, so both checks can be done with a single If statement:

Dim mystring as string = Nothing

If mystring IsNot Nothing AndAlso mystring.Length > 100 Then 'Code A1

Elself mystring.GetType Is GetType(Integer) Then

'Code B2 End If

The preceding code would fail on the first comparison because mystring has only been initialized to Nothing, meaning that by definition it doesn't have a length. Note also that the second condition will fail because you know that mystring isn't of type Integer.