Comparison Operators

Compare two values or a range of values.


Normal comparison
value_1 comparison_op value_2
Range comparison
value_1 { < | <= } value_2 { { < | <= } value_n }…
Tolerant comparison
value_1 ~comparison_op value_2 [ ~comparison_op value_n ]… Tol tolerance


Necessary expression
Necessary; One of the comparison operators: <, <=, >, >=, =, or <>.
Necessary expression of a type compatible with that of value_1
Necessary for a range comparison, an expression of a type compatible with that of value_1 and value_2
Necessary if a tolerance is specified; any of the comparison operators preceded by a tilde (~). Not all comparisons need to be tolerant; however, all tolerant comparisons must use the same tolerance within a single expression.
Necessary after a tolerant ~comparison_op; a numeric expression to specify the floating-point error tolerance of the preceding expression. See Tol Clause for more information.


OperatorTrue ifFalse if
< (Less than)value_1 < value_2value_1 >= value_2
<= (Less than or equal to)value_1 <= value_2value_1 > value_2
> (Greater than)value_1 > value_2value_1 <= value_2
>= (Greater than or equal to)value_1 >= value_2value_1 < value_2
= (Equal to)value_1 = value_2value_1 <> value_2
<> (Not equal to)value_1 <> value_2value_1 = value_2

Comparing numbers


Comparing floating-point numbers

The Tol keyword can be used to make floating-point comparisons tolerant to rounding errors. A comparison expression without this clause might produce unexpected results due to the subtle ways that tiny rounding errors can get introduced by calculations. The tolerance value represents the largest amount the two values can differ and still be considered equal.

Comparing strings


Range comparisons

You can test if a value falls within a range of values using a special form of the comparison syntax. You write this by using two "less than" operators to separate the three values, placing the lower value first, the highest value last, and the value to be tested in the middle. This form is borrowed from mathamatics and should be immediately recognizable by a mathematician.

The form is semantically equivalent to the following:

value_1 { < | <= } value_2 And value_2 { < | <= } value_3

If you were to write code like the above and value_2 were an expression with side effects, the results could be unpredictable. The range comparison ensures that each subexpression is evaluated only once.

Only the operators < and <= are valid in this form of comparison. However, it is not a syntax error to use other comparison operators in an analogous form, though doing so is usually considered a programming error. Each comparison would be evaluated from left to right, then the Boolean result will be compared to the next value in the chain. This rarely makes sense in code.

Comparing objects

Unlike other values, objects can only be compared for equality with the Is operator.

Three-way comparison

The operator <=> is available only as part of the Conditional Operator. It tests if one value is less than, equal to, or greater than a second value. A different name for it is the “spaceship operator” because of its appearance.


If x ~= 4.5 Tol 1e-2 Then
End If

If 0.0 ~<= y ~<= 1.0 Tol 1e-8 Then
End If