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.
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?
"Apples" = "Oranges"
"Apples" = "Apples"
" Apples" == "Apples"
100 = 1E2
100 \= 1E2
100 \== 1E2
ANSWERS
The language processor produces the following sentences:
Today's lunch cost the same or less than yesterday's.
Today's lunch cost the same or less than yesterday's.
Today's lunch cost more than yesterday's.
The expressions result in the following. Remember
0
is
false and
1
is true.