Comparison Operators
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.
Comparison operators can compare numbers
or strings and perform evaluations, such as:
- 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)
The
more commonly used comparison operators are as follows:
- 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 book 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
section
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
- "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.
- 0
- 1
- 0 (The first " Apples" has a space.)
- 1
- 0
- 1