ICS 101 Lab 3 Hossain Arif ICS Dept Logical Operations ICS 101 Lab 3 Hossain Arif ICS Dept
Objective What are Logical Operations What are Relational Operations What are Logical Expressions Logical Expressions Evaluation Examples Exercises
Arithmetic Operations(1) Arithmetic operators Precedence Math Notation Example Operation FORTRAN Operator 1 XY X ** Y Exponentiation ** 2 X x Y X * Y Multiplication * X ÷ Y X / Y Division / 3 X + Y Addition + X - Y Subtraction - Parentheses () have the highest precedence
Arithmetic Operations(2) Two restrictions No two operators must appear consecutively 2 * -3 should be written as 2 * (-3) Exponential operator must not be used to raise a negative number to a real exponent (-2.0) ** 1.5 and (-3) ** 2.3 are not allowed in FORTRAN
Arithmetic Operations(3) Integer operations An operator between two integer operands Produces integer results In division, the fraction part is truncated Examples: 50 – 23 3 ** 2 5 * 7 8 / 3 9 / 10 17 / 6 * 6
Arithmetic Operations(4) Real operations An operator between two real operands Produces real results Examples: 50.0 – 23.0 3.0 ** 2.0 5.0 * 7.0 9. / 10. 9.3 / 3.2
Arithmetic Operations(5) Mixed-mode operations An operator between an integer operand and a real operand Produces real results Examples: 50 – 23.0 3.0 ** 2 , 3 ** 2.0 65.7 / 7 8.0 / 2 9 / 10.
Logical Operations(1) Logical Operations Logical Operators: .NOT. P Evaluate to either .TRUE. or .FALSE. Logical Operators: .NOT. P P .OR. Q P .AND. Q Q P .TRUE. .FALSE. .NOT. operator has the highest precedence (1) followed by .AND. (2) then .OR. (3)
Logical Operations(2) Relational operators Example Math Operator X .EQ. Y = .EQ. N .NE. 8 ≠ .NE. P .GT. 7.3 > .GT. SM .GE. TOT ≤ .GE. A+B .LT. A*2.0 < .LT. NUM .LE. C ≥ .LE. Relational operators have lower precedence than arithmetic operators and higher precedence than logical operators
Logical Operations(3) Logical expressions Evaluated to .TRUE. or .FALSE. May contain different types of variables and operators Examples: .NOT. FLAG .AND. X * Y .GT. Z .OR. X + Y .GT. Z Where X has a value of 3.0, Y has a value of 5.0, Z has a value of 10.0, and FLAG is logical variable with .FALSE. value K / L * L .EQ. K Where K and L are integers
Examples .NOT. FLAG .AND. X * Y .GT. Z .OR. X + Y .GT. Z .NOT. FLAG .AND. 15.0 .GT. 10.0 .OR. 8.0 .GT. 10.0 .NOT. FLAG .AND. .TRUE. .OR. .FALSE. .TRUE. .AND. .TRUE. .OR. .FALSE. .TRUE. .OR. .FALSE. .TRUE.
Precedence Summary Arithmetic Operators (1) Relational Operators (2) Exponentiation (I) Multiplication / Division (II) Addition / Subtraction (III) Relational Operators (2) Logical Operators (3) .NOT. (#) .AND. (##) .OR. (###) To change Precedence (Priority) use Parentheses
Exercise Find the value for each of the following expressions X= 3.0, Y = 5.0, Z = 10.0, FLAG = .FALSE. .NOT. FLAG .OR. FLAG X .GT. Y – Z / 2.0 X * Z .EQ. 20.0 .OR. FLAG .AND. .NOT. Z .EQ. 5.0 .NOT. FLAG .AND. FLAG