Relational Operations

From QB64 Wiki

Revision as of 05:26, 23 June 2011 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

QB64 supports several relational operations, which are binary operations that test numeric or string values and return an INTEGER value representing a boolean true (-1) or false (0) result. These operations are primarily used in expressions where a condition is required, such as the IF...THEN statement.

List of relational operations

The following table describes the relational operations, where A is the left-hand side operand, and B is the right-hand side operand:

Relational Operations
Operation Description
A = B Tests if A is equal to B.
A <> B Tests if A is not equal to B; equivalent to (NOT (A = B)).
A < B Tests if A is less than B.
A > B Tests if A is greater than B.
A <= B Tests if A is less than or equal to B; equivalent to (NOT (A > B)).
A >= B Tests if A is greater than or equal to B; equivalent to (NOT (A < B)).

Comparing numerical values and variables

For numeric operands, such as INTEGER or DOUBLE values, the relational operations behave as one would expect. For example, the expression (-3.4 < 1.2) evaluates to true, and the expression (50 = 100) evaluates to false.

TODO: describe how numeric operands are converted if their types are different ?

Comparing string values and variables

For string operands, the = and <> operators test, respectively, if the operands are of the same or differing length, and that each of the corresponding characters match or don't match. For example, the three expressions ("abc" = "abc"), ("abc" <> "abd") and ("abc" <> "abcdef") all evaluate to true.

The < and > operators find the first set of non-matching characters and test if the byte values of those characters are less than or greater than each other, respectively. If one string is a prefix of the other, < returns true if the left-hand side string is shorter than the right-hand side string, while > returns true if the left-hand side string is longer than the right-hand side string. For example, the expressions ("abc" < "abd") and ("abcdef" > "abc") both evaluate to true.

Comparing user-defined type variables

Variables of a user-defined type (UDT) cannot be used as operands to the relational operators, but numeric or string type fields can be used as described above. For example:

TYPE T
a AS INTEGER
END TYPE
TYPE U
b AS INTEGER
END TYPE
DIM x AS T : x.a = 10
DIM y AS U : y.b = 20
PRINT x < y ' <- error: type mismatch
PRINT x.a < y.b ' <- outputs "-1"

Boolean values

The INTEGER values for true and false are such that the bitwise Logical Operations, such as NOT, AND and OR can be used to invert and combine test results. For example, the expression (NOT (10 >= 20)) evaluates to true, while the expression ((1 < 2) AND ("three" = "four")) evaluates to false.

See also



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page