Presentation is loading. Please wait.

Presentation is loading. Please wait.

23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --

Similar presentations


Presentation on theme: "23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --"— Presentation transcript:

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

2 23 March, 2000 Program logical flow In structured program, three basic control structures: sequenceselection(Ch 3)repetition (Ch 4) Statement sequence1 Logical- expression Logical- expression Statement sequence2 Statement sequence2 Statement sequence2 Statement sequence1 Statement sequence2 Statement sequence3

3 23 March, 2000 Logical Expressions Logical- expression Statement sequence1 Statement sequence2 Statement sequence3 Control structures Simple or compound Constructs: IF IF-ELSE IF CASE

4 23 March, 2000 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 23 March, 2000 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 23 March, 2000 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’ ‘6’

7 23 March, 2000 Compound Logical Expressions Two logical expressions combined with a logical operator expression 1 logical-operator expression 2 expression 1 and expression 2 are logical expressions

8 23 March, 2000 Logical Operators.NOT..AND..OR..EQV..NEQV. 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

9 23 March, 2000 Truth Tables for NOT, AND, & OR PQP.AND. QP.OR. Q.TRUE..TRUE..TRUE..TRUE..TRUE..FALSE..FALSE..TRUE..FALSE..TRUE..FALSE..TRUE..FALSE..FALSE. Q.NOT. Q.TRUE..FALSE..FALSE..TRUE.

10 23 March, 2000 Truth Table for EQV & NEQV PQP.EQV. Q P.NEQV. Q.TRUE..TRUE..TRUE..FALSE..TRUE..FALSE..FALSE..TRUE..FALSE..TRUE..FALSE..TRUE..FALSE..FALSE..TRUE..FALSE.

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

12 23 March, 2000 Examples N = = 5.OR. N = = 10.NOT. ( (M Y) ) suppose M=3, N=4, X=10, Y=14, and Z=4:.NOT. ( (3 14)).NOT. (.TRUE..AND. (14 > 14) ).NOT. (.TRUE..AND..FALSE.).NOT. (.FALSE.).TRUE.

13 23 March, 2000 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 23 March, 2000 Simple IF statement IF (logical-expression) then Executes procedure if TRUE Skips procedure if FALSE Ends with END IF TRUE FALSE

15 23 March, 2000 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 23 March, 2000 Logical IF Statement IF (logical-expression) statement No THEN or ENDIF e.g., IF (X>=0) PRINT *, X

17 23 March, 2000 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 IF Procedure1 Procedure2 THEN ELSE END IF TRUE FALSE

18 23 March, 2000 ! 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


Download ppt "23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --"

Similar presentations


Ads by Google