Comparing values

There are many times when you need to compare two values to determine whether or not a particular condition exists. When making a comparison, what you are really doing is making a statement that will either be True or False. For example, the statement "My dog is a cat" evaluates to False. However, the statement "My dog weighs more than my cat" may evaluate to True.

Description

Symbol

Example

Result

Equality

=

5 = 5

True

Inequality

<>

5 <> 5

False

Greater Than

>

6 > 5

True

Less Than

<

6 < 5

False

Greater Than or Equal To

>=

6 >= 5

True

Less Than or Equal To

<=

6 <= 5

False

Text and Boolean values can also be used for comparisons. Text comparisons are case-insensitive and alphabetical. This means that "Steve" and "steve" are equal. But "diane" is less than "steve" because "diane" falls alphabetically before "steve". If you need to make case-sensitive or lexicographic comparisons, you use the String.Compare method like this:

Var dog As String = "Dog"
Var cat As String = "Cat"

Var result As Integer
result = dog.Compare(cat, ComparisonOptions.CaseSensitive)
// result = 1, because "Dog" > "Cat"

To do a case-sensitive comparison:

Var dog As String = "Dog"
Var cat As String = "aCat"

Var result As Integer
result = dog.Compare(cat, ComparisonOptions.CaseSensitive, Locale.Raw)
// result = -1, because "Dog" < "aCat" when case-sensitive

In addition, there is a way to determine whether two floating point numbers are close enough to be considered equal. This is used to account for the imprecision of operations such as floating point division. Use the Double.Equals to do this comparison.

For example, if you are comparing 10000 to a value and specify x=1, then the acceptable values are 10000.000000000002, 10000.0 and 9999.999999999998.

Var d As Double = 10000
If d.Equals(compareValue, 1) Then
  // take action here...
End If

Logical comparisons

You can test more than one comparison at a time using the And, Or, and Not operators. When passed Boolean values, these operators determine whether the expression is True or False.

And operator

Use the And operator when you need to know if all comparisons evaluate to True. In the example below, if the variable x contains 10 then the expression evaluates to False because 10 is not both greater than 1 and less than 5:

x > 1 And x < 5

Or operator

Use the Or operator when you need to know if any of the comparisons evaluate to True. In the example below, if the variable x contains 10 then the expression evaluates to True:

x > 1 Or x < 5

This is because 10 is greater than 1. The fact that it is not less than 5 does not matter because you only care if at least one of the comparisons is True.

Xor operator

Use the Xor operator when you need to know whether any of the comparisons evaluate to True, but not all of them. In the example below, if the variable x contains 10 then the expression evaluates to True.

x > 1 Xor x < 5

This is because only one of the comparisons is True. However, if x contains 3 then the above expression returns False because both sub expressions are True.

Not operator

Use the Not operator to reverse the value of a boolean variable. For example, this returns True when x is equal to or greater than 0:

Not x < 0

Truth table

A truth table can help you determine what the results of these various comparisons are.

Expression 1

Expression 2

And

Or

Xor

True

True

True

True

False

True

False

False

True

True

False

True

False

True

True

False

False

False

False

True

Bitwise comparisons

Sometimes it is helpful, usually in more advanced situations, to be able to compare the actual bits that make up Integers. You can do this using Bitwise class method comparisons, which work by treating the Integer as a binary number. With a binary number, you can then compare each individual bit to get a new set of bits, which results in a new number.

Bitwise comparisons work with the And, Or and Xor operators when they are used with Integers instead of Boolean expressions.

For example, consider the decimal numbers 5 and 3. Written in binary, they are:

  • 101 (5)

  • 011 (3)

To evaluate 5 Xor 3, you do the comparison on each bit. So for this example:

  • 1 Xor 0 = 1

  • 0 Xor 1 = 1

  • 1 Xor 1 = 0

The new binary value is 110, which is decimal 6.

So this is the final result of the equation: 5 Xor 3 = 6.

The Not operator can also be used to compare Integers. If you pass an integer to Not, it reverses each bit value.

To evaluate Not 5:

  • Not 1 = 0

  • Not 0 = 1

  • Not 1 = 0

The new binary value is 010, which is decimal 2.

Bitwise tables

Results for And, Or and Xor:

Bit 1

Bit 2

And

Or

Xor

1

1

1

1

0

1

0

0

1

1

0

1

0

1

1

0

0

0

0

1

Result for Not:

Bit 1

Not Bit 1

1

0

0

1