# Comparison Operators

Compare two values or a range of values.

## Syntax

```
value_1 comparison_op value_2
```

```
value_1 { < | <= } value_2 { { < | <= } value_n }...
```

```
value_1 ~comparison_op value_2 [ ~comparison_op value_n ]... Tol tolerance
```

## Parts

`value_1`- Necessary expression
`comparison_op`- Necessary; One of the comparison operators:
`<`,`<=`,`>`,`>=`,`=`, or`<>`. `value_2`- Necessary expression of a type compatible with that of
`value_1` `value_n`- Necessary for a range comparison, an expression of a type compatible with that of
`value_1`and`value_2` `~``comparison_op`- 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. `tolerance`- 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.

## Instructions

Operator | True if | False if |
---|---|---|

< (Less than) | value_1 < value_2 | value_1 >= value_2 |

<= (Less than or equal to) | value_1 <= value_2 | value_1 > value_2 |

> (Greater than) | value_1 > value_2 | value_1 <= value_2 |

>= (Greater than or equal to) | value_1 >= value_2 | value_1 < value_2 |

= (Equal to) | value_1 = value_2 | value_1 <> value_2 |

<> (Not equal to) | value_1 <> value_2 | value_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 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:

```
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 `<` 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
```