1 / 21

Conditional Statements

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.

derrickl
Download Presentation

Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Conditional Statements Part 1 Module 2 Concepts Covered: Conditional Statements Serially Arranged Conditionals Nested Conditionals Boolean Expressions

  2. Conditional Statements Concept: Conditional Statements

  3. “control flow” and sequential execution Open the trash lid Close trash lid Drop empty can in trash

  4. “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

  5. 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

  6. 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

  7. Conditional Statements Application: Serially Arranged Conditionals

  8. Serial Arrangements Condition #1 code #1b code #1a Condition #2 code #2a code #2b code #3

  9. Conditional Statements Application: Nested Conditionals

  10. Nested Arrangements Condition #1 Condition #2 Condition #3 Code #1a Code #1b Code #2a Code #2b Code #3

  11. Conditional Statements Concept: Boolean Expressions

  12. 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

  13. 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

  14. Concept of Truth Table ( a < b ) AND ( a > 0) ( a < b ) OR ( a > 0 ) NOT ( a < b )

  15. 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

  16. 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”

  17. 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”

  18. 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”

  19. 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”

  20. 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”

  21. 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”

More Related