210 likes | 220 Views
Learn the basics of conditional statements, including serially arranged conditionals, nested conditionals, and boolean expressions. Practice opening/closing a trash lid based on certain conditions. Understand control flow and the concept of sequential execution.
E N D
Conditional Statements Part 1 Module 2 Concepts Covered: Conditional Statements Serially Arranged Conditionals Nested Conditionals Boolean Expressions
Conditional Statements Concept: Conditional Statements
“control flow” and sequential execution Open the trash lid Close trash lid Drop empty can in trash
“control flow” and conditional execution Is the trashcan’s lid already up? YES TRUE NO FALSE Open the trash lid Drop empty can in trash Close trash lid
IF-THEN vs. IF-THEN-ELSE statements Is the trashcan’s lid already up? YES TRUE NO FALSE Yell: “Who left the trash open!!!???” Open the trash lid Drop empty can in trash Close trash lid
YES TRUE NO FALSE Is the trashcan’s lid already up? Flowcharts vs. Pseudo Code IF <condition> THEN <statements> ELSE <statements> ENDIF Open the trash lid Drop empty can in trash Close trash lid
Conditional Statements Application: Serially Arranged Conditionals
Serial Arrangements Condition #1 code #1b code #1a Condition #2 code #2a code #2b code #3
Conditional Statements Application: Nested Conditionals
Nested Arrangements Condition #1 Condition #2 Condition #3 Code #1a Code #1b Code #2a Code #2b Code #3
Conditional Statements Concept: Boolean Expressions
What’s in a conditional expression Is the trashcan’s lid already up? NO FALSE What is a boolean expression? evaluates into TRUE or FALSE only e.g. Comparing variables / values a < b a <= 3 + b a + b >= 999 YES TRUE Open the trash lid Drop empty can in trash Close trash lid
NO FALSE What’s in a conditional expression Is the trashcan’s lid already up? 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 evaluation Do not assume lazy evaluation Until you KNOW your programming language specifics YES TRUE Open the trash lid Drop empty can in trash Close trash lid
Concept of Truth Table ( a < b ) AND ( a > 0) ( a < b ) OR ( a > 0 ) NOT ( a < b )
Advices Is the trashcan’s lid already up? NO FALSE ( a < b ) AND ( a > 0) ( a < b ) OR ( a > 0 ) NOT ( a < b ) Do not assume left to right evaluation Do not assume lazy evaluation Until you KNOW your programming language specifics YES TRUE Open the trash lid Drop empty can in trash Close trash lid
Multi-terms Boolean expressions to simplify (a<b) AND (a>0) NO FALSE IF ( a < b ) AND ( a > 0) THEN display “OK” ELSE display “KO” ENDIF display “done” YES TRUE Display: “OK” Display: “KO” Display: “done”
Multi-terms Boolean expressions to simplify IF ( a < b ) THEN IF ( a > 0) THEN display “OK” ELSE display “KO” ENDIF ELSE display “KO” ENDIF display “done” (a<b) YES TRUE NO FALSE YES TRUE (a>0) NO FALSE Display: “OK” Display: “KO” Display: “KO” Display: “done”
Multi-terms Boolean expressions to simplify (a<b) OR (a>0) NO FALSE IF ( a < b ) OR ( a > 0) THEN display “OK” ELSE display “KO” ENDIF display “done” YES TRUE Display: “OK” Display: “KO” Display: “done”
Multi-terms Boolean expressions to simplify IF ( a < b ) THEN display “OK” ELSE IF ( a > 0) THEN display “OK” ELSE display “KO” ENDIF ENDIF display “done” (a<b) YES TRUE NO FALSE (a>0) NO FALSE YES TRUE Display: “OK” Display: “OK” Display: “KO” Display: “done”
Multi-terms Boolean expressions to simplify (a<b) XOR (a>0) NO FALSE IF ( a < b ) XOR ( a > 0) THEN display “OK” ELSE display “KO” ENDIF display “done” YES TRUE Display: “OK” Display: “KO” Display: “done”
Multi-terms Boolean expressions to simplify (a<b) IF ( 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” NO FALSE YES TRUE NO FALSE YES TRUE NO FALSE YES TRUE (a>0) (a>0) KO OK OK KO Display: “done”