CS1001%20Lecture%206 - PowerPoint PPT Presentation

About This Presentation
Title:

CS1001%20Lecture%206

Description:

Executes procedure 1 if true. ELSE it executes procedure 2 if false ... Check if discriminant is nonnegative. If ! it is, calculate and display the roots. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 19
Provided by: plum1
Learn more at: http://web.cs.wpi.edu
Category:
Tags: 20lecture | cs1001

less

Transcript and Presenter's Notes

Title: CS1001%20Lecture%206


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

2
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 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. lt is less than
  • .GT. gt is greater than
  • .EQ. is equal to
  • .LE. lt is less than or equal to
  • .GE. gt is greater than or equal to
  • .NE. / is not equal to

6
Examples
  • 5.2 lt27 result is .TRUE.
  • (B2) gt (2.0AC)
  • 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 lt F, Alta, aa lt ac, ab lt aa,
    balt aa, 8 gt 6

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

8
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
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. ( (MltN) .AND. (XZ gt Y) )
  • suppose M3, N4, X10, Y14, and Z4
  • .NOT. ( (3lt4) .AND. (104 gt 14))
  • .NOT. ( .TRUE. .AND. (14 gt 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 lt 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 PiLength2 Perimet
er2.0PiLength Print , For Circle, Area ,
Area, Perimeter , Perimeter END
IF
16
Logical IF Statement
  • IF (logical-expression) statement
  • No THEN or ENDIF
  • e.g., IF (Xgt0) 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 gt 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
Write a Comment
User Comments (0)
About PowerShow.com