Comparison

Operator

Syntax

Compares two values: equal, different, less, greater, less or equal, greater or equal. Numbers compare by value, text by character codes.

<a> = <b>
<a> <> <b>
<a> < <b>
<a> > <b>
<a> <= <b>
<a> >= <b>
<a> == <b>
<a> != <b>

Also written : =, <>, <, >, <=, >=

Parts

<a>A number or a string.
<b>A value of the same kind. A string is never equal to a number.

Good to know

  • In IF, WHILE and UNTIL a comparison is simply true or false. Stored in a variable it is 1 or 0. Passed straight to a command it is TRUE or FALSE: Console(Str(a > 3)) prints true.
  • On the right of an assignment, = compares: same = (a = 5) does not change a. The parentheses are optional, but clearer.
  • Inside the parentheses of a command call, = assigns: Console(Str(a = 1)) sets a to 1, silently. Compare in a variable first, or in an IF.
  • = and <> are strict: "5" is not 5. Convert first with Val() or Str(). == and != are accepted but compare loosely, converting text and numbers; prefer = and <>.
  • Text compares in order of character codes, so case counts: "B" < "a" is true, and "apple" <> "Apple".
  • Decimals are compared exactly: 0.1 + 0.2 = 0.3 is false. Compare the difference with Abs() against a small margin.
  • 1 < x < 5 means (1 < x) < 5, always true. Write x > 1 AND x < 5.
  • The reversed spellings of other BASICs, =<, => and ><, compile but the program does not start. Write <=, >= and <>.

Example

a = 5same = (a = 5)differ = (a <> 5)Console(Str(same) + " " + Str(differ))   ' 1 0loose = ("5" = 5)Console(Str(loose))   ' 0IF a >= 5 THEN Console("five or more")   ' five or more
In the manual : Constants and operators

Commands that go with it