Relational Operators

Relational operators allow you to compare the values of two expressions, to obtain a Boolean result of TRUE or FALSE.  Although they can be used in any numeric expression (for example, a = (b > c) / 13), the numeric results returned by relational operators are generally used in an IF or other decision statements, to make a judgment regarding program flow.

PowerBASIC relational operators






5  =  5

<>, ><


5 <> 6


Less than

5  <  6


Greater than

6  >  5

<=, =<

Less than or equal to

5 <= 6

>=, =>

Greater than or equal to

6 >= 5

When arithmetic and relational operators are combined in an expression, arithmetic operations are always evaluated first.  For example, 4 + 5 < 4 * 3 evaluates to TRUE (non-zero), because the arithmetic operations (addition and multiplication) are carried out before the relational operation.  This then tests the truth of the assertion 9 < 12.

Strings and relational operators  

PowerBASIC lets you compare string data.  String expressions can be tested for equality, as well as for "greater than" and "less than" alphanumeric ordering.

Two string expressions are equal if and only if they contain exactly the same characters in exactly the same order.  For example:

a$ = "CAT"

x1% = (a$ = "CAT") : x2% = (a$ = "CATS") : x3% = (a$ = "cat")

String ordering is based on two criteria: first, the ASCII values of the characters they contain, and second, the length of the strings.

For example, the letter A is less than the letter B because the ASCII code for A, 65, is less than the code for B, 66.  Note, however, that B is less than a because the ASCII code for each lowercase letter is greater than the corresponding uppercase character (exactly 32 greater).  When comparing mixed uppercase and lowercase information, use the UCASE$ or LCASE$ functions to keep case differences from interfering with the test.

city1$ = "Seattle"

city2$ = "Tucson"

IF UCASE$(city1$) > UCASE$(city2$) THEN

  city$ = city1$


  city$ = city2$


city1$ = UCASE$(city1$)

city2$ = UCASE$(city2$)

IF city1$ > city2$ THEN

  city$ = city1$


  city$ = city2$


Note the difference between the two sets of statements.  In the first case, the string variables city1$ and city2$ are converted to uppercase for the comparison only, so the first IF/THEN returns Tucson.  In the second case, the conversion is performed on the variables themselves, so the result will be TUCSON.

Length is important only if both strings are identical up to the length of the shorter string, in which case the shorter one evaluates as less than the longer one; for example, CAT is less than CATS.

The ARRAY SORT and ARRAY SCAN statements allow you to specify whether lower case characters are to be treated as uppercase for comparison purposes.  You can also specify a string that explicitly determines the sorting order for all 256 ASCII characters.


See Also

Arithmetic Operators

Operator Precedence