COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Conditional Statements...

Post on 18-Jan-2018

216 views 0 download

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

transcript

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

1

Conditional Statements

Concepts Covered:

Conditional StatementsSerially Arranged ConditionalsNested ConditionalsBoolean Expressions

Part 1Module 2

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

2

Conditional Statements

Concept: Conditional Statements

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

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

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!!!???”

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

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

7

Conditional Statements

Application: Serially Arranged Conditionals

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

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

9

Conditional Statements

Application: Nested Conditionals

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

COP 2510 Programming Concepts Alessio GasparBSAS Industrial Operations

11

Conditional Statements

Concept: Boolean Expressions

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

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

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 )

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

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”

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

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”

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

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”

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