21
COP 2510 Programming Concepts Alessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged Conditionals Nested Conditionals Boolean Expressions Part 1 Module

COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

Embed Size (px)

DESCRIPTION

COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 3 “control flow” and sequential execution Open the trash lid Drop empty can in trash Close trash lid

Citation preview

Page 1: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

1

Conditional Statements

Concepts Covered:

Conditional StatementsSerially Arranged ConditionalsNested ConditionalsBoolean Expressions

Part 1Module 2

Page 2: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

2

Conditional Statements

Concept: Conditional Statements

Page 3: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

3

“control flow” and sequential execution

Open the trash lid

Drop empty can in trash

Close trash lid

Page 4: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

4

“control flow” and conditional execution

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

Page 5: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

5

IF-THEN vs. IF-THEN-ELSE statements

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

Yell: “Who left the

trash open!!!???”

Page 6: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

6

Flowcharts vs. Pseudo Code

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

IF <condition>THEN

<statements>ELSE

<statements>ENDIF

Page 7: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

7

Conditional Statements

Application: Serially Arranged Conditionals

Page 8: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

8

Serial Arrangements

code #1a

code #2a

code #1b

Condition #1

Condition #2

code #2b

code #3

Page 9: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

9

Conditional Statements

Application: Nested Conditionals

Page 10: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

10

Nested Arrangements

Code #1a

Code #2a

Code #1b

Condition #1

Condition #2

Code #2b

Code #3

Condition #3

Page 11: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

11

Conditional Statements

Concept: Boolean Expressions

Page 12: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

12

What’s in a conditional expression

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

What is a boolean expression?

evaluates into TRUE or FALSE only

e.g. Comparing variables / valuesa < b a <= 3 + b a + b >= 999

Page 13: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

13

What’s in a conditional expression

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

We can have several terms in a conditional expression

( a < b ) AND ( a > 0) ( a < b ) OR ( a > 0 )NOT ( a < b )

Do not assume left to right evaluationDo not assume lazy evaluationUntil you KNOW your programming language specifics

Page 14: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

14

Concept of Truth Table

( a < b ) AND ( a > 0) ( a < b ) OR ( a > 0 ) NOT ( a < b )

Page 15: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

15

Advices

Open the trash lid

Drop empty can in trash

Close trash lid

Is the trashcan’slid already up?

YESTRUE

NOFALSE

( a < b ) AND ( a > 0)

( a < b ) OR ( a > 0 )

NOT ( a < b )

Do not assume left to right evaluationDo not assume lazy evaluation

Until you KNOW your programming language specifics

Page 16: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

16

Multi-terms Boolean expressions to simplify

Display: “OK”

Display:“done”

(a<b) AND (a>0)

YESTRUE

NOFALSE

IF ( a < b ) AND ( a > 0) THEN

display “OK” ELSE

display “KO”ENDIF

display “done”

Display: “KO”

Page 17: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

17

Multi-terms Boolean expressions to simplify

Display: “OK”

Display:“done”

(a<b)YESTRUE

NOFALSE

IF ( a < b ) THEN

IF ( a > 0)THEN

display “OK” ELSE

display “KO”ENDIF

ELSE display “KO”

ENDIF

display “done”

Display: “KO”

(a>0)

Display: “KO”

YESTRUE

NOFALSE

Page 18: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

18

Multi-terms Boolean expressions to simplify

Display: “OK”

Display:“done”

(a<b) OR (a>0)

YESTRUE

NOFALSE

IF ( a < b ) OR ( a > 0) THEN

display “OK” ELSE

display “KO”ENDIF

display “done”

Display: “KO”

Page 19: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

19

Multi-terms Boolean expressions to simplify

Display: “OK”

Display:“done”

(a<b)YESTRUE

NOFALSE

IF ( a < b ) THEN

display “OK” ELSE

IF ( a > 0)THEN

display “OK” ELSE

display “KO”ENDIF

ENDIF

display “done”

Display: “KO”

(a>0)

Display: “OK”

YESTRUE

NOFALSE

Page 20: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

20

Multi-terms Boolean expressions to simplify

Display: “OK”

Display:“done”

(a<b) XOR (a>0)

YESTRUE

NOFALSE

IF ( a < b ) XOR ( a > 0) THEN

display “OK” ELSE

display “KO”ENDIF

display “done”

Display: “KO”

Page 21: COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements Concepts Covered: Conditional Statements Serially Arranged

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

21

Multi-terms Boolean expressions to simplify

KO

Display:“done”

(a<b)YESTRUE

NOFALSEIF ( a < b )

THEN IF ( a>0 )THEN display “KO” ELSE display “OK”ENDIF

ELSE IF ( a > 0)THEN display “OK” ELSE display “KO”ENDIF

ENDIF

display “done”

KO

(a>0)

OK

YESTRUE

NOFALSE

(a>0)

YESTRUE

NOFALSE

OK