Syntax
|
Expression1 [< | > | <= | >= | <> | =] expression2
|
Description
|
Comparison operators return True or False depending on the operator.
|
Comments
|
The comparison operators are listed in the following table:
|
|
Operator
|
Returns True If
|
|
>
|
expression1 is greater than expression2
|
|
<
|
expression1 is less than expression2
|
|
<=
|
expression1 is less than or equal to expression2
|
|
>=
|
expression1 is greater than or equal to expression2
|
|
<>
|
expression1 is not equal to expression2
|
|
=
|
expression1 is equal to expression2
|
|
This operator behaves differently depending on the types of the expressions, as shown in the following table:
|
|
If one
expression is
|
and the other
expression is
|
Then
|
|
Numeric
|
Numeric
|
A numeric comparison is performed (see below).
|
|
String
|
String
|
A string comparison is performed (see below).
|
|
Numeric
|
String
|
A compile error is generated.
|
|
Variant
|
String
|
A string comparison is performed (see below).
|
|
Variant
|
Numeric
|
A variant comparison is performed (see below).
|
|
Null variant
|
Any data type
|
Returns Null.
|
|
Variant
|
Variant
|
A variant comparison is performed (see below).
|
|
String Comparisons
If the two expressions are strings, then the operator performs a text comparison between the two string expressions, returning True if expression1 is less than expression2. The text comparison is case-sensitive if Option Compare is Binary; otherwise, the comparison is case-insensitive.
When comparing letters with regard to case, lowercase characters in a string sort greater than uppercase characters, so a comparison of "a" and "A" would indicate that "a" is greater than "A".
Numeric Comparisons
When comparing two numeric expressions, the less precise expression is converted to be the same type as the more precise expression.
Dates are compared as doubles. This may produce unexpected results as it is possible to have two dates that, when viewed as text, display as the same date when, in fact, they are different. This can be seen in the following example:
|
|
Sub Main()
Dim date1 As Date
Dim date2 As Date
date1 = Now
date2 = date1 + 0.000001 'Adds a fraction of a second.
MsgBox date2 = date1 'Prints False (the dates are different).
MsgBox date1 & "," & date2 'Prints two dates that are the same.
End Sub
|
|
Variant Comparisons
When comparing variants, the actual operation performed is determined at execution time according to the following table:
|
|
If one
variant is
|
and the other
variant is
|
Then
|
|
Numeric
|
Numeric
|
The variants are compared as numbers.
|
|
String
|
String
|
The variants are compared as text.
|
|
Numeric
|
String
|
The number is less than the string.
|
|
Null
|
Any other data type
|
Null
|
|
Numeric
|
Empty
|
The number is compared with 0.
|
|
String
|
Empty
|
The string is compared with a zero-length string.
|
Example
|
Sub Main()
'Tests two literals and displays the result.
If 5 < 2 Then
MsgBox "5 is less than 2."
Else
MsgBox "5 is not less than 2."
End If
'Tests two strings and displays the result.
If "This" < "That" Then
MsgBox "'This' is less than 'That'."
Else
MsgBox "'That' is less than 'This'."
End If
End Sub
|
See Also
|
Operator Precedence (topic); Is (operator); Like (operator); Option Compare (statement).
|