ICS I01 - Computer programming

                                   LAB # 3

                               (Logical Operations)

 

Objectives

 

 

Logical Operators

 

      .AND            .OR.              .NOT.

 

Examples

 

P

Q

P .AND. Q

P .OR. Q

.NOT. P

.FALSE.

.FALSE.

.FALSE.

.FALSE.

.TRUE.

.FALSE.

.TRUE.

.FALSE.

.TRUE.

.TRUE.

.TRUE.

.FALSE.

.FALSE.

.TRUE.

.FALSE.

.TRUE.

.TRUE.

.TRUE.

.TRUE.

.FALSE.

 

Priority of Logical Operators

  1. .NOT. 

  2. .AND.

  3. .OR.

 

 

Relational Operators

 

Operator

Meaning

Example

.LT.

Less than

 X  .LT.  Y

.LE.

Less than or equal to

Y  .LE.  Z

.EQ.

            Equal to 

A  .EQ.  B

.NE.

Not equal to

A  .NE.  C

.GT.

Greater than

B  .GT.  D

.GE.

Greater than or equal to

D  .GE.  F

 

Examples

 

If A = 4.7, B = 4.3 and  C = 3.4

A .GT. B    evaluates to .TRUE.

B .LT. C     evaluates to .FALSE.

 

 

 

Logical Expressions

 

 

Example

 

Given that X has a value of 3.0, Y has a value of  5.0, Z has a value of 10.0, and FLAG

is a logical variable with .FALSE. Value,  evaluate the following FORTRAN expression:

 

                GOAL = .NOT. FLAG .AND. X*Y .GT. Z .OR. X + Y .GT. Z

 

 

Exercise 1

A > 5

B < 8

9 <= X <= 15

18 > Y >= 12

17 > Z > 33

(X > 6) AND (Y >= 14)

 

   Exercise 2

    Complete the following program by making the assignment:

    GOAL = .NOT. FLAG .AND. X*Y .GT. Z .OR. X + Y .GT. Z

    X = 4.5 , Y= 6.5 , Z = 11.0

 

            LOGICAL  FLAG , GOAL

            REAL  X, Y, Z

            FLAG = .FALSE.

            X  =  ---------------

            Y  =  ---------------

            Z  =  ---------------

            PRINT*, ' THE OUTPUT IS = ' , GOAL

            END

 

 

Exercise 3

    Complete the following program by making the assignment:

    GOAL = X*Z .EQ. 20.0  .OR. FLAG .AND. .NOT. Z .EQ. 25.0

    X = 5.5 , Z = 12.0 , FLAG = .FALSE.

 

            LOGICAL   -------------------------------

            REAL         -------------------------------

            GOAL =      -------------------------------

            PRINT*, ' THE OUTPUT IS = ' , ------------------

            END