overview program flow decision / branching / selection structures true & false in python...

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

Post on 18-Dec-2015

231 views

Category:

Documents


1 download

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

Sequential - stacked

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

Comparison operators & Boolean expressions

With earlier versions of Python:• True = 1• False = 0

if …

If true, do. If not true (false), do not do.

if …is a single-alternative decision structure

Syntax:

Indentedstatements!

True or False …

if … else

if … else is a dual-alternative decision structure

UML Activity Diagram

If true, do. If not true (false), do something else.

if … elif … else

Syntax:

if … elif … else is a multi-alternative decision structure

Nested if

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

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.

Order of evaluation (precedence)

String comparisons

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

Boolean expression as a value

Same result as above if … else

Overview

Program flowDecision / branching / selection structures

True & False in PythonComparison operators & Boolean expressionsif … if … else if … elif … else