Logical and Relational Expressions

[ Logical Operands ] [ Relational Operands ] [ Logical and Relational Expressions ]

Logical and relational operands are used in questions that have a true/false answer. Expressions composed of logical and relational operands play a central role in ALADDIN programming constructs for the branching and looping control of problem solving procedure.


LOGICAL OPERANDS

ALADDIN supports three logical operands on physical quantities. They are:

    OPERAND           DESCRIPTION
    ====================================================

    ||                Logical "or"
    &&                Logical "and"
     !                Logical "not"

    ====================================================

Expressions involving logical operands will evaluate to either true or false. In keeping with other programming languages, "true" is represented by a non-zero number, and "false" by zero.

Example 1 : In our first example,

    print "2 && 3   : ", 2 && 3, " (true)  \n";
    print "2 && 0   : ", 2 && 0, " (false) \n";
    print "2 || 0   : ", 2 || 0, " (true)  \n";
    print "0 || 0   : ", 0 || 0, " (false) \n";
    print "!0       : ", !0    , " (true)  \n";
    print "!2       : ", !2    , " (false) \n";
we simply evaluate logical operands for a variety of non-zero and zero number combinations. The generated output is:
    2 && 3   :          1  (true)  
    2 && 0   :          0  (false) 
    2 || 0   :          1  (true)  
    0 || 0   :          0  (false) 
    !0       :          1  (true)  
    !2       :          0  (false)

Example 2 : Logical operand also work on physical quantities having units. For example, the script

    print "2 cm && 3 cm : ", 2 cm && 3 cm, " (true)  \n";
    print "0 cm/sec && 3 cm^2  : ", 0 cm && 3 cm, " (false)  \n";
generates the output
    2 cm && 3 cm :           1  (true)  
    0 cm/sec && 3 cm^2  :    0  (false)
In each case, the logical operand in applied to the numerical component of the physical quantity alone (the units play no role in the operand's evaluation).


RELATIONAL OPERANDS

The relational operands are:

    OPERAND           DESCRIPTION
    ====================================================

    ==                Test for "identical equality"
     >                Greater than
     <                Less than
    >=                Greater than or equal to
    <=                Less than or equal to

    ====================================================

Example 3 : Expressions involving relational opeators may be applied directly to physical quantities -- for example, the script:

    print "2 cm == 3 cm : ", 2 cm == 3 cm, " (false)  \n";
    print "2 cm >  3 cm : ", 2 cm >  3 cm, " (false)  \n";
    print "2 cm <  3 cm : ", 2 cm <  3 cm, " (true)   \n";
    print "2 cm <= 3 cm : ", 2 cm <= 3 cm, " (true)   \n";
generates the output
    2 cm == 3 cm :          0  (false)  
    2 cm >  3 cm :          0  (false)  
    2 cm <  3 cm :          1  (true)   
    2 cm <= 3 cm :          1  (true)

Example 4 : A key feature of ALADDIN's units package is checking of compatible units before a logical or relational operand is evaluated. In the following script, we show how sets of mixed units may be used in relational expressions -- the script of code:

    print "20 cm  == 200 mm : ", 20 cm == 200 mm, " (true)  \n";
    print "12 in   >   1 ft : ", 12 in  >   1 ft, " (false)  \n";
    print "36 in  => 100 cm : ", 36 in  > 100 cm, " (false)  \n";
generates the output
    20 cm  == 200 mm :          1  (true)  
    12 in   >   1 ft :          0  (false)  
    36 in  => 100 cm :          0  (false)  
Everything works as expected. Now suppose that we make a small error, as in the script:
    x = 20 cm;
    y = 20 cm/sec;

    print "20 cm => 20 cm/sec : ", x > y , " (huh ???)  \n";
ALADDIN generates the output
    20 cm => 20 cm/sec :
    FATAL ERROR >> "In Quantity_Gt() : Inconsistent Dimensions"
and terminates its execution.


LOGICAL AND RELATIONAL EXPRESSIONS

Of course logical and relational operands may be combined. Here are a couple of examples:

Example 5 : The script

    x = 20 cm > 10 cm;
    y = 20 sec >= 100 sec ;

    print "x : ", x , " (true)  \n";
    print "y : ", y , " (false)  \n";
    print "x || y : ", x || y , " (true)  \n";
    print "x && y : ", x && y , " (false)  \n";
generates the output
    x :          1  (true)  
    y :          0  (false)  
    x || y :          1  (true)  
    x && y :          0  (false)
Note : Further examples may be found in the sections on branching constructs and looping constructs.


Developed in April 1996 by Mark Austin
Last Modified June 21, 1996
Copyright © 1996, Mark Austin, Department of Civil Engineering, University of Maryland