Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …

Similar presentations


Presentation on theme: "Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …"— Presentation transcript:

1 Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if … elif … else

2 Program flow overview Programs in any language have three types of flow Sequential Decision/Branching/Selection Repetition Combined in two ways Stacking Nesting Next week

3 Sequential - stacked

4 Decision / Branching / Selection Branching depends on a conditional expression Conditional expressions must evaluate to True or False How does the programming language represent True or False? How does the language make comparisons?

5 True & False in Python True valuesFalse Values Non-zero numbers (positive & negative) 0 Non-empty stringEmpty string Non-empty listEmpty list Non-empty tupleEmpty tuple Non-empty dictionaryEmpty dictionary not None None TrueFalse

6 Comparison operators & Boolean expressions With earlier versions of Python: True = 1 False = 0

7 if … If true, do. If not true (false), do not do. if …is a single-alternative decision structure Syntax: Indented statements!

8 True or False …

9 if … else if … else is a dual-alternative decision structure UML Activity Diagram If true, do. If not true (false), do something else.

10 if … elif … else Syntax: if … elif … else is a multi-alternative decision structure

11 Nested if Only execute the “nested if” when the “parent if” is true

12 Logical operators: and, or, not Short-circuit evaluation For “and”, if left side is False, right side is not evaluated. For “or”, if left side is True, right side is not evaluated.

13 Order of evaluation (precedence)

14 String comparisons Python uses ASCII codes to determine character values that are used in Boolean expressions.

15 Boolean expression as a value Same result as above if … else

16 Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if … elif … else


Download ppt "Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …"

Similar presentations


Ads by Google