Comparison operators

Comparison operators can compare numbers or strings and perform evaluations. Expressions that use comparison operators do not return a number value as do arithmetic expressions. Comparison expressions return either 1, which represents true, or 0, which represents false.

These are typical comparisons:
  • Are the terms equal? ( A = Z )
  • Is the first term greater than the second? ( A > Z )
  • Is the first term less than the second? ( A < Z )
For example, if A = 4 and Z = 3, then the results of the previous comparison questions are:
  • (A = Z) Does 4 = 3? 0 (False)
  • (A > Z ) Is 4 > 3? 1 (True)
  • (A < Z ) Is 4 < 3? 0 (False)

Commonly used comparison operators

Operator
Meaning
=
Equal
==
Strictly Equal
\ =
Not equal
\ ==
Not strictly equal
>
Greater than
<
Less than
> <
Greater than or less than (same as not equal)
> =
Greater than or equal to
\ <
Not less than
< =
Less than or equal to
\ >
Not greater than
Note: The NOT character ( ¬ ) is synonymous with the backslash ( \ ). You can use the two characters interchangeably according to availability and personal preference. This information uses the backslash ( \ ) character.

The strictly equal and equal operators

When two expressions are strictly equal, everything including the blanks and case (when the expressions are characters) is exactly the same.

When two expressions are equal, they are resolved to be the same. The following expressions are all true.
'WORD' = word      /* returns 1 */
'word ' \== word   /* returns 1 */
'word' == 'word'   /* returns 1 */
4e2 \== 400        /* returns 1 */
4e2 \= 100         /* returns 1 */

Using comparison expressions

You often use a comparison expression in an IF...THEN...ELSE instruction. The following example uses an IF...THEN...ELSE instruction to compare two values. For more information about this instruction, see IF…THEN…ELSE instructions.
Figure 1. Example Using a Comparison Expression

/****************************** REXX *********************************/
/* This program compares what you paid for lunch for two             */
/* days in a row and then comments on the comparison.                */
/*********************************************************************/

PARSE PULL yesterday     /* Gets yesterday's price from input stream */
PARSE PULL today         /* Gets today's price */
IF today > yesterday THEN /* lunch cost increased */
   SAY "Today's lunch cost more than yesterday's."
ELSE                    /* lunch cost remained the same or decreased */ 
   SAY "Today's lunch cost the same or less than yesterday's."

Exercises: using comparison expressions

Based on the preceding example of using a comparison expression, what result does the language processor produce from the following lunch costs?
Yesterday's Lunch
Today's Lunch
4.42
3.75
3.50
3.50
3.75
4.42
What is the result ( 0 or 1 ) of the following expressions?
  1. "Apples" = "Oranges"
  2. "Apples" = "Apples"
  3. " Apples" == "Apples"
  4. 100 = 1E2
  5. 100 \= 1E2
  6. 100 \== 1E2
 
ANSWERS
  1. The language processor produces the following sentences:
    1. Today's lunch cost the same or less than yesterday's.
    2. Today's lunch cost the same or less than yesterday's.
    3. Today's lunch cost more than yesterday's.
  2. The expressions result in the following. Remember 0 is false and 1 is true.
    1. 0
    2. 1
    3. 0 (The first " Apples" has a space.)
    4. 1
    5. 0
    6. 1