180 likes | 322 Views
CS1001 Lecture 6. Logical Expressions Logical Operators Selective Execution Web page -- http://www.cs.wpi.edu/~nitin/cs1001/. Logical- expression. Logical- expression. Program logical flow. In structured program, three basic control structures:
E N D
CS1001 Lecture 6 • Logical Expressions • Logical Operators • Selective Execution Web page -- http://www.cs.wpi.edu/~nitin/cs1001/
Logical- expression Logical- expression Program logical flow In structured program, three basic control structures: sequence selection(Ch 3) repetition (Ch 4) Statement sequence2 Statement sequence1 Statement sequence1 Statement sequence2 Statement sequence2 Statement sequence3 Statement sequence2
Logical- expression Logical Expressions Control structures Simple or compound Constructs: IF IF-ELSE IF CASE Statement sequence1 Statement sequence2 Statement sequence3
Simple Logical Expressions • Logical constants or variables • Form: expression1 relational-operator expression2 • where expression1 and expression2 can be • numeric 5.2, 27, -35.67 • Character ‘open’ ‘close’ • Logical .TRUE.
Relational Operators • Operators that show relationship between the two variables .LT. < is less than .GT. > is greater than .EQ. == is equal to .LE. <= is less than or equal to .GE. >= is greater than or equal to .NE. /= is not equal to
Examples • 5.2 <=27 result is .TRUE. • (B**2) >= (2.0*A*C) • Relationship between character strings • depends on internal representation of the characters (see Appendix A of text for ASCII coding) • ‘A’, ‘B’, …, ‘Z’ are 65,66,…,90 • ‘a’,’b’,…’z’ are 97,98,…,122 • ‘A’ < ‘F’, ‘A’<‘a’, ‘aa’ < ‘ac’, ‘ab’ < ‘aa’, ‘ba’< ‘aa’, ‘8’ > ‘6’
Compound Logical Expressions • Two logical expressions combined with a logical operator • expression1 logical-operator expression2 • expression1and expression2 are logical expressions
Logical Operators Logical negation Conjunction, true only if both are true Disjunction, true if one or both are true Equivalence, true if both true or both false Nonequivalence, true if one is true and the other is false. .NOT. Q P .AND. Q P .OR Q P .EQV. Q P .NEQV. Q .NOT. .AND. .OR. .EQV. .NEQV.
Truth Tables for NOT, AND, & OR Q .NOT. Q .TRUE. .FALSE. .FALSE. .TRUE. P Q P .AND. Q P .OR. Q .TRUE. .TRUE. .TRUE. .TRUE. .TRUE. .FALSE. .FALSE. .TRUE. .FALSE. .TRUE. .FALSE. .TRUE. .FALSE. .FALSE. .FALSE. .FALSE.
Truth Table for EQV & NEQV P Q P .EQV. Q P .NEQV. Q .TRUE. .TRUE. .TRUE. .FALSE. .TRUE. .FALSE. .FALSE. .TRUE. .FALSE. .TRUE. .FALSE. .TRUE. .FALSE. .FALSE. .TRUE. .FALSE.
Precedence Rule • Arithmetic operations (and functions) • Relational operations • Logical operations in the order .NOT. , .AND., .OR. , .EQV. (or .NEQV.)
Examples • N = = 5 .OR. N = = 10 • .NOT. ( (M<=N) .AND. (X+Z > Y) ) suppose M=3, N=4, X=10, Y=14, and Z=4: .NOT. ( (3<=4) .AND. (10+4 > 14)) .NOT. ( .TRUE. .AND. (14 > 14) ) .NOT. ( .TRUE. .AND. .FALSE.) .NOT. (.FALSE.) .TRUE.
Selective Execution • Simple IF statement • Logical IF statement • General IF and ELSE construct • Nested IF statements • Compound IF and ELSE IF construct • Case Next Week
Simple IF statement • IF (logical-expression) then • Executes procedure if TRUE • Skips procedure if FALSE • Ends with END IF TRUE FALSE
Examples IF (Discriminant < 0) THEN PRINT *, "Discriminant is", Discriminant PRINT *, ”There are two complex roots & &to this equation” END IF ! Calculate area and perimeter of circle IF (Figure == “C”) THEN Area = Pi*Length**2 Perimeter=2.0*Pi*Length Print *, “For Circle, Area = “, Area, & “ Perimeter = “, Perimeter END IF
Logical IF Statement • IF (logical-expression) statement • No THEN or ENDIF • e.g., IF (X>=0) PRINT *, X
General IF and ELSE • IF (logical-expression) THEN • Executes procedure 1 if true • ELSE it executes procedure 2 if false • Ends with END IF Begin THEN IF TRUE FALSE Procedure1 ELSE Procedure2 END IF
! Check if discriminant is nonnegative. If ! it is, calculate and display the roots. ! Otherwise display the value of the ! Discriminant and a no-real-roots message. IF (Discriminant >= 0) THEN Discriminant = SQRT(Discriminant) Root_1 = (-B + Discriminant) / (2.0 * A) Root_2 = (-B - Discriminant) / (2.0 * A) PRINT *, "The roots are", Root_1, Root_2 ELSE PRINT *, "Discriminant is", Discriminant PRINT *, "There are no real roots" END IF END PROGRAM QuadraticEquations_1