@DATABASE "ARB13"
@INDEX "ARB:Misc/Index.Text/Main"
@HELP "ARB:Misc/Help/Main"
@NODE "Main" "ARexx For Beginners - Article 13 - Logical Operators"

@{B}@{U}@{JCENTER}AREXX FOR BEGINNERS

ARTICLE 13 - LOGICAL OPERATORS

BY FRANK BUNTON@{UB}@{UU}

@{" COPYRIGHT © FRANK P. BUNTON 1995-1998 " LINK "ARB:Misc/Read_Me_First!!/Copyright"}
@{JLEFT}

A full list of operators is contained in @{"Appendix B" LINK "ARB:Appendices/App-B_Operators/MAIN"27} which covers Arithmetic,
Logical, Comparison and concatenation operators.

In this article we will cover only Logical Operators.
@{JCENTER}
@{" General Comments       " LINK "General"}
@{" How Operators Work     " LINK "How"}
@{" More Than 2 Conditions " LINK "More"}

@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "General" "Article 13 - Logical Operators - General Comments"

@{B}@{U}@{JCENTER}LOGICAL OPERATORS

General Comments@{UB}@{UU}
@{JLEFT}
Logical Operators are also called @{"BOOLEAN" LINK "ARB:Misc/Glossary/Boolean"} operators. They are:-

     @{U}Operator @{UU}  @{U}Operation     @{UU}     @{U}Priority@{UU}

        ~          NOT                8
        &          AND                2
        |          OR                 1
        ^ or &&    Exclusive OR       1
                   (also called XOR)

Priorities were discussed in @{"Article 9" LINK "ARB:Articles_01-10/09.Arithmetic_Operators/Priorities"}.

(See also @{"More Than 2 Conditions" LINK "More"}.)

The symbol for the "Exclusive OR" can either ^ or &&. For simplicity and
to avoid confusion I would suggest that you stick to the ^ symbol. I mention
the && symbol only in case you come across it somewhere and wonder what
it is.

We have already seen in @{"Article 11" LINK "ARB:Articles_11-20/11.Comparison_Operators/Available"} how the NOT operator (~) is used for
"Not Equal" or "Not Greater Than" etc.

The other three logical operators can be used in situations where we wish
to use an IF...THEN...ELSE to test for more than one condition in an
expression, as in Line 6 of @{"Example13-1" LINK "How"61} below and in line 7 of @{"Example12-5" LINK "ARB:Articles_11-20/12.Conditional_Statements/Tossing"36}
in Article 12 - Conditional Statements.

Examples are:-

  IF X = 2 & Y = 2 THEN .....         IF X = 2 @{B}AND@{UB} Y = 2
  IF X = 2 | Y = 2 THEN .....         IF X = 2 @{B}OR@{UB}  Y = 2
  IF X = 2 ^ Y = 2 THEN .....         IF X = 2 @{B}XOR@{UB} Y = 2

You can also have more complex expressions with more than two conditions
such as:-

  IF X = 2 &   Y = 2  | Z = 1 THEN .....

  IF X = 2 @{B}AND@{UB} Y = 2 @{B}OR@{UB} Z = 1

I will discuss these sort of statements under the heading
@{"More Than 2 Conditions" LINK "More"}.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "How" "Article 13 - Logical Operators - How Operators Work"

@{B}@{U}@{JCENTER}LOGICAL OPERATORS

How Operators Work@{UB}@{UU}
@{JLEFT}
In discussing how these logical operators work, we should consider that
in:-

  IF X = 2 & Y = 2 THEN .....

the "X = 2 & Y = 2" is the total expression that is evaluated by the IF
but that it is consists of two conditions (X = 2 and Y = 2) which are
separated by a logical operator (&).

The operators work in these ways:-

@{U}Operator@{UU}@{U} Total expression is true:-@{UU}

@{B}AND (&)@{UB} - only if BOTH the conditions in it are true.

@{B}OR (|)@{UB}  - if only ONE of the conditions is true
        - but also if BOTH the conditions are true.

@{B}XOR (^)@{UB} - if only ONE of the conditions is true.
        - but NOT if both are true

These points are illustrated in the following table:-

   @{U}X@{UU}  @{U}Y@{UU}  @{U}X=2 & Y=2@{UU}  @{U}X=2 | Y=2@{UU}  @{U}X=2 ^ Y=2@{UU}

   1  1   False      False      False
   1  2   False      True       True
   2  1   False      True       True
   2  2   True       True       False
   3  1   False      False      False
   3  2   False      True       True

This table can be produced by the program @{B}Example13-1@{UB} which also illustrates
the use of the three operators. @{"Click here" LINK "ARB:Articles_11-20/Example13-1.rexx/MAIN"} to read it all at once. It
is described line by line below:-

@{B}Lines 1-3@{UB} display the headings:-

   @{B}1 SAY 'Testing Logical Operators' ; SAY

   2 SAY 'X Y X=2 & Y=2 X=2 | Y=2 X=2 ^ Y=2'
   3 SAY '- - --------- --------- ---------'@{UB}

@{B}Lines 4 & 5@{UB}

   @{B}4 DO x = 1 to 3
   5   DO y = 1 to 2@{UB}

set up two @{B}loops@{UB} of programming code. The loops end with the END instructions
at lines 13 and 14. This allows the coding from line 6 to line 12 to be
operated @{B}SIX@{UB} times. It is operated for:-

- Y values of 1 then 2 while X = 1
- Y values of 1 then 2 while X = 2
- Y values of 1 then 2 while X = 3

@{B}Line 6@{UB}:-

   @{B}6     IF x = 2 & Y = 2 THEN T1 = 'True '@{UB}

tests to see if both X=2 @{B}AND@{UB} Y=2. If so a symbol named "T1" (for Test1)
is assigned a value of "True". If not, "T1" is assigned a value of "False"
in @{B}Line 7@{UB}:-

   @{B}7     ELSE T1 = 'False'@{UB}

@{B}Lines 8 and 9@{UB}:-

   @{B}8     IF x = 2 | Y = 2 THEN T2 = 'True '
   9     ELSE T2 = 'False'@{UB}

are similar to Lines 6 and 7. They test for X=2 @{B}OR@{UB} Y=2 and assign "True"
or "False" to T2.

@{B}Lines 10 and 11@{UB}:-

  @{B}10     IF x = 2 ^ Y = 2 THEN T3 = 'True '
  11     ELSE T3 = 'False'@{UB}

test for X=2 @{B}Exclusive OR@{UB} Y=2 and make assignments to T3.

@{B}Line 12@{UB}:-

  @{B}12     SAY x '' y ' ' T1 ' ' T2 ' ' T3@{UB}

then prints the results.

@{B}Lines 13 and 14@{UB}:-

  @{B}13   END
  14 END@{UB}

end the loops started in Lines 4 and 5.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE

@NODE "More" "Article 13 - Logical Operators - More Then 2 Conditions"

@{B}@{U}@{JCENTER}LOGICAL OPERATORS

More Then 2 Conditions@{UB}@{UU}
@{JLEFT}
I mentioned above that you can have more than two conditions in the one
IF ... THEN statement, e.g.:-

  IF X = 2 | Y = 2 & Z = 1 THEN .....

This is where the @{"priorities" LINK "General"} shown at the start of this article come into
effect.

The priorities of the operators are:-

   & (AND)    2
   | (OR)     1

Therefore, in the line:-

  IF X = 2 | Y = 2 & Z = 1 THEN .....

the @{B}first@{UB} "true or false" test is applied to:-

  Y = 2 & Z = 1

If X=2 @{B}AND@{UB} Y=2 are both true then the whole of:-

  Y = 2 & Z = 1

is true.

If either or both of them are false, then the whole is false.

Having obtained a "true" or "false" for the above, the @{B}second@{UB} evaluation
is for one of:-

  X = 2 | TRUE

  X = 2 | FALSE

If "X=2" is TRUE  then, in both cases, the whole is TRUE

If "X=2" is FALSE then, in both cases, the whole is FALSE

To change the order of evaluation, put part of it in brackets. For
example:-

  IF (X = 2 | Y = 2) & Z = 1 THEN .....

Now the expressions:-

  X = 2 | Y = 2

is evaluated first even though | has a lower priority than &.

This can be illustrated by Example13-2:-

  /* Example13.2 */

  X = 2 ; Y = 2 ; Z = 2

  IF X = 2 | Y = 2 & Z = 1 THEN SAY 'YES'
  ELSE SAY 'NO'

  IF (X = 2 | Y = 2) & Z = 1 THEN SAY 'YES'
  ELSE SAY 'NO'

When you RX this, you should see this displayed:-

  YES
  NO

which illustrates how the parentheses have changed the priorities of the
comparison operators.


@{JCENTER}=== End of Text ===
@{JLEFT}
@ENDNODE
