overview program flow decision / branching / selection structures true & false in python...
Post on 18-Dec-2015
231 views
TRANSCRIPT
Overview
Program flowDecision / branching / selection structures
True & False in PythonComparison operators & Boolean expressionsif … if … else if … elif … else
Program flow overview
Programs in any language have three types of flow
Sequential
Decision/Branching/Selection
Repetition
Combined in two waysStacking
Nesting
Next week
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?
True & False in Python
True values False Values
Non-zero numbers(positive & negative)
0
Non-empty string Empty string
Non-empty list Empty list
Non-empty tuple Empty tuple
Non-empty dictionary
Empty dictionary
not None None
True False
if …
If true, do. If not true (false), do not do.
if …is a single-alternative decision structure
Syntax:
Indentedstatements!
if … else
if … else is a dual-alternative decision structure
UML Activity Diagram
If true, do. If not true (false), do something else.
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.
String comparisons
Python uses ASCII codes to determine character values that are used in Boolean expressions.