1 / 18

CS1001 Lecture 6

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:

rossa
Download Presentation

CS1001 Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS1001 Lecture 6 • Logical Expressions • Logical Operators • Selective Execution Web page -- http://www.cs.wpi.edu/~nitin/cs1001/

  2. 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

  3. Logical- expression Logical Expressions Control structures Simple or compound Constructs: IF IF-ELSE IF CASE Statement sequence1 Statement sequence2 Statement sequence3

  4. 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.

  5. 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

  6. 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’

  7. Compound Logical Expressions • Two logical expressions combined with a logical operator • expression1 logical-operator expression2 • expression1and expression2 are logical expressions

  8. 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.

  9. 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.

  10. 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.

  11. Precedence Rule • Arithmetic operations (and functions) • Relational operations • Logical operations in the order .NOT. , .AND., .OR. , .EQV. (or .NEQV.)

  12. 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.

  13. 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

  14. Simple IF statement • IF (logical-expression) then • Executes procedure if TRUE • Skips procedure if FALSE • Ends with END IF TRUE FALSE

  15. 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

  16. Logical IF Statement • IF (logical-expression) statement • No THEN or ENDIF • e.g., IF (X>=0) PRINT *, X

  17. 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

  18. ! 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

More Related