Logical (Boolean) Operators
Logical expressions, like comparison expressions, return 1 (true) or 0 (false) when processed. Logical operators combine two comparisons and return 1 or 0 depending on the results of the comparisons.
The logical operators are:
- Operator
- Meaning
- &
-
AND
Returns 1 if both comparisons are true. For example:
(4 > 2) & (a = a) /* true, so result is 1 */ (2 > 4) & (a = a) /* false, so result is 0 */ - |
- Inclusive OR
Returns 1 if at least one comparison is true. For example:
(4 > 2) | (5 = 3) /* at least one is true, so result is 1 */ (2 > 4) | (5 = 3) /* neither one is true, so result is 0 */ - &&
- Exclusive OR
Returns 1 if only one comparison (but not both) is true. For example:
(4 > 2) && (5 = 3) /* only one is true, so result is 1 */ (4 > 2) && (5 = 5) /* both are true, so result is 0 */ (2 > 4) && (5 = 3) /* neither one is true, so result is 0 */ - Prefix \,¬
- Logical NOT
Negates—returning the opposite response. For example:
\ 0 /* opposite of 0, so result is 1 */ \ (4 > 2) /* opposite of true, so result is 0 */
Using logical expressions
You can use logical
expressions in complex conditional instructions and as checkpoints
to screen unwanted conditions. When you have a series of logical
expressions,
for clarification, use one or more sets of parentheses to enclose
each expression.
IF ((A < B) | (J < D)) & ((M = Q) | (M = D))
THEN ....
The
following example uses logical operators to make a decision.
Figure 1. Example Using Logical Expressions
/****************************** REXX
********************************/
/* This
program
receives arguments for a complex logical expression */
/* that determines whether a person should go skiing. The first */
/* argument is a season and the other two can be 'yes' or 'no'. */
/********************************************************************/
PARSE ARG season snowing broken_leg
IF ((season = 'WINTER') | (snowing ='YES')) & (broken_leg ='NO')
THEN SAY 'Go skiing.'
ELSE
SAY 'Stay home.'
When arguments passed to this example are
SPRING YES NO
,
the IF clause translates as follows:
IF ((season = 'WINTER') | (snowing ='YES')) &
(broken_leg ='NO') THEN
\______________/ \____________/ \_____________/
false true true
\___________________/ /
true /
\_____________________________/
true
As a result,
when you run the
program
, it
produces the result:
Go skiing.
Exercises: using logical expressions
A student
applying to colleges has decided to evaluate them according to the
following specifications:
IF (inexpensive | scholarship) & (reputable | nearby)
THEN
SAY "I'll consider it."
ELSE
SAY "Forget it!"
A college is inexpensive, did not offer a scholarship, is reputable, but is more than 1000 miles away. Should the student apply?
ANSWER
Yes. The conditional
instruction works out as follows:
IF (inexpensive | scholarship) & (reputable | nearby)
THEN ...
\__________/ \___________/ \_________/ \______/
true false true false
\___________/ \_________/
true true
\_________________________/
true