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 

 

Write the following logical expressions in FORTRAN 77 language.  Fill in the table below or on a piece of paper.

 

Logical Expression

FORTRAN 77 Form

X > 10

 

X ≤ Y

 

0.0 ≤ X < 1.0

 

X ≠ 0  OR Y = 0

 

NOT (15 > X ≥ 5) AND Y = 0.0

 

 

 

Exercise 2

 

Write a FORTRAN program that will evaluate and print the result of all the logical expressions in Exercise 1

after taking the values of X and Y from the user.  X is a REAL variable.  Y is an INTEGER variable.

 

Sample Run 1:

Sample Run 2:

 
 
Exercise 3

    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 4

    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