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,WHILEandUNTILa comparison is simply true or false. Stored in a variable it is 1 or 0. Passed straight to a command it isTRUEorFALSE: Console(Str(a > 3)) printstrue. - On the right of an assignment,
=compares:same = (a = 5)does not changea. The parentheses are optional, but clearer. - Inside the parentheses of a command call,
=assigns: Console(Str(a = 1)) setsato 1, silently. Compare in a variable first, or in anIF. =and<>are strict:"5"is not5. 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.3is false. Compare the difference with Abs() against a small margin. 1 < x < 5means(1 < x) < 5, always true. Writex > 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 moreIn the manual : Constants and operators