Comparison Operators

Compare two values or a range of values.

Syntax

Normal comparison
value1 comparisonOp value2
Range comparison
value1 { < | <= } value2 { { < | <= } valueN }...
Tolerant comparison
value1 ~comparisonOp value2 [ ~comparisonOp valueN ]... Tol tolerance

Parts

value1
Required expression
comparisonOp
Required; One of the comparison operators: <, <=, >, >=, =, or <>.
value2
Required expression of a type compatible with that of value1
valueN
Required for a range comparison, an expression of a type compatible with that of value1 and value2
~comparisonOp
Required 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.
tolerance
Required after a tolerant ~comparisonOp; a numeric expression to specify the floating-point error tolerance of the preceding expression. For more information, see Tol Clause.

Details

OperatorTrue ifFalse if
< (Less than)value1 < value2value1 >= value2
<= (Less than or equal to)value1 <= value2value1 > value2
> (Greater than)value1 > value2value1 <= value2
>= (Greater than or equal to)value1 >= value2value1 < value2
= (Equal to)value1 = value2value1 <> value2
<> (Not equal to)value1 <> value2value1 = value2

Comparing numbers

Comparing floating-point numbers

The Tol keyword can be used to make floating-point comparisons tolerant to rounding errors. A comparison expression lacking 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:

value1 { < | <= } value2 And value2 { < | <= } value3

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

Only the < and <= operators 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 using the Is operator.

Examples

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

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