Comparison Operators

Compare two values or a range of values.


Usual comparison

value_1 comparison_op value_2

Range comparison

value_1 { < | <= } value_2 { { < | <= } value_n }…

Tolerant comparison

value_1 tolerant_comparison value_2 [ tolerant_comparison value_n ]… Tol tolerance


An expression to compare.
One of the comparison operators: <, <=, >, >=, =, or <>.
An expression of a type compatible with that of value_1.
For a range comparison, an expression of a type compatible with that of value_1 and value_2.
One of the comparison operators with an optional tilde (~) before it. Not all comparisons must be tolerant, but all tolerant comparisons use the same tolerance in the full expression.
After Tol, a numeric expression to specify the floating-point error tolerance of the comparisons. See Tol Clause for more information.


Operator True if False if
< (Less than) value_1 < value_2 value_1 >= value_2
<= (Less than or equal) value_1 <= value_2 value_1 > value_2
> (More than) value_1 > value_2 value_1 <= value_2
>= (More than or equal) value_1 >= value_2 value_1 < value_2
= (Equal) value_1 = value_2 value_1 <> value_2
<> (Not equal) value_1 <> value_2 value_1 = value_2

Comparing numbers


Comparing floating-point numbers

You can use the keyword Tol to make floating-point comparisons with a tolerance to rounding errors. A comparison expression without this clause could possibly give incorrect results because of how calculated results can have small rounding errors. The tolerance value is the maximum the two values can be different but compare as equal.

Comparing strings


Range comparisons

You can find if a value is in a range with a special syntax. You use two operators from the set of < and <= to connect the three values. You put the values in the sequence (1) lower value, (2) test value, and (3) higher value. This syntax is almost the same as that used by mathamatics.

It is almost the same as what follows:

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

If you wrote code the same as above and value_2 is an expression with side effects, then the results could be unknown. The range comparison makes sure that each expression does not calculate more than one time.

Only the operators < and <= are correct in this type of comparison. It is not a syntax error to use other comparison operators the same. But it is usually a programming error. Each comparison will evaluate from left to right, then the Boolean result will be compared to the next value in the chain. Thus this usually makes incorrect code.

Comparing objects

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

Other comparison operators

The operator <=> is available only as part of the Conditional Operator. It does a test of two values to find if one is less than, equal to, or more than the other. A different name for it is the “spaceship operator” because of how it looks.


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

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