Conditionals & Control Flow [Andersen, Gries, Lee, Marschner, - - PowerPoint PPT Presentation

conditionals control flow
SMART_READER_LITE
LIVE PREVIEW

Conditionals & Control Flow [Andersen, Gries, Lee, Marschner, - - PowerPoint PPT Presentation

CS 1110: Introduction to Computing Using Python Lecture 8 Conditionals & Control Flow [Andersen, Gries, Lee, Marschner, Van Loan, White] Announcements: Assignment 1 Due tonight at 11:59pm. Suggested early submit deadline of 2pm.


slide-1
SLIDE 1

Conditionals & Control Flow

Lecture 8

CS 1110:

Introduction to Computing Using Python

[Andersen, Gries, Lee, Marschner, Van Loan, White]

slide-2
SLIDE 2

Announcements: Assignment 1

  • Due tonight at 11:59pm.
  • Suggested early submit deadline of 2pm.
  • Set CMS notifications to receive automatic emails when

a grade is changed.

  • First round of feedback should be out by Monday.
  • If your A1 is not perfect, your first grade will be a 1.
  • This is a counter for how many times you have submitted.
  • It is not a permanent grade, can resubmit until March 2nd.
  • Read section 2.3 of A1 carefully to understand how you

can revise.

2/13/17 Conditionals & Control Flow 2

slide-3
SLIDE 3

Announcements

  • Please do not post code to Piazza1
  • Review the announcements from the end of Lecture 6

for policies:

http://www.cs.cornell.edu/courses/cs1110/2017sp/lectures/02-14-17/presentation-06.pdf

1actually violating academic integrity rules because you are showing code to others

2/13/17 Conditionals & Control Flow 3

slide-4
SLIDE 4

Methods: Functions Tied to Classes

  • Method: function tied to class
  • Method call looks like a function

call preceded by a variable name: ⟨variable⟩.⟨method⟩(⟨arguments⟩)

  • Example: p.distanceTo(q)
  • Just like we saw for strings
  • s = 'abracadabra'
  • s.index('a')
  • Are strings objects?

id3 x 5.0 y 2.0 z 3.0 id3 p

Point3

Actually, yes.

2/13/17 Conditionals & Control Flow 4

slide-5
SLIDE 5

Name Resolution

  • ⟨object⟩.⟨name⟩ means
  • Go the folder for object
  • Look for attribute/method name
  • If missing, check class folder
  • Class folder is a shared folder
  • Only one for the whole class
  • Shared by all objects of class
  • Stores common features
  • Typically where methods are

id3 x 5.0 y 2.0 z 3.0 id3 p

Point3

x, y, z distanceTo(other) abs() Point3

id4 x 7.4 y 0.0 z 0.0 id4 q

Point3

2/13/17 Conditionals & Control Flow 5

slide-6
SLIDE 6

Structure vs. Flow

Program Structure

  • Order in which statements

are written in scripts and modules

  • Not necessarily the order in

which Python executes them

Control Flow

  • Order in which statements are

actually executed at runtime

  • Statements may be:
  • skipped
  • executed more than once

Have already seen this difference with functions

2/13/17 Conditionals & Control Flow 6

slide-7
SLIDE 7

Structure vs. Flow: Example

Program Structure

def foo(): print 'Hello' # Script Code foo() foo() foo()

Control Flow

C:\> python foo.py 'Hello' 'Hello' 'Hello'

2/13/17 Conditionals & Control Flow 7

This statement listed only once Statement executed 3 times

slide-8
SLIDE 8

Conditionals: If-Statements Format

if <boolean-expression>: <statement> … <statement>

Example

# Put x in z if it is positive if x > 0: z = x

2/13/17 Conditionals & Control Flow 8

Execution:

if ⟨Boolean-expression⟩ is true, then execute all of the statements indented directly underneath (until first non-indented statement)

slide-9
SLIDE 9

What gets printed?

a = 0 print a

2/13/17 Conditionals & Control Flow 9

prints 0

slide-10
SLIDE 10

What gets printed?

a = 0 a = a + 1 print a

2/13/17 Conditionals & Control Flow 10

prints 1

slide-11
SLIDE 11

What gets printed?

a = 0 if a == 0: a = a + 1 print a

2/13/17 Conditionals & Control Flow 11

prints 1

slide-12
SLIDE 12

What gets printed?

a = 0 if a == 1: a = a + 1 print a

2/13/17 Conditionals & Control Flow 12

prints 0

slide-13
SLIDE 13

What gets printed?

a = 0 if a == 1: a = a + 1 a = a + 1 print a

2/13/17 Conditionals & Control Flow 13

prints 1

slide-14
SLIDE 14

What gets printed?

a = 0 if a == 0: a = a + 1 a = a + 1 print a

2/13/17 Conditionals & Control Flow 14

prints 2

slide-15
SLIDE 15

What gets printed?

a = 0 if a == 0: a = a + 1 if a == 0: a = a + 1 a = a + 1 print a

2/13/17 Conditionals & Control Flow 15

A: 0 B: 1 C: 2 D: 3 E: I do not know

CORRECT Executed Executed Skipped Executed

slide-16
SLIDE 16

Conditionals: If-Else-Statements Format

if <boolean-expression>: <statement> … else: <statement> …

Example

# Put max of x and y in z if x > y: z = x else: z = y

2/13/17 Conditionals & Control Flow 16

Execution:

if ⟨Boolean-expression⟩ is true, then execute statements indented under if; otherwise execute the statements indented under else

slide-17
SLIDE 17

Conditionals: “Control Flow” Statements

if b : s1 # statement s3 if b : s1 else: s2 s3

2/13/17 Conditionals & Control Flow 17

s1 s3 s2 b s1 s3 b

Branch Point: Evaluate & Choose Statement: Execute

Flow

Program only takes one path each execution True False True False

slide-18
SLIDE 18

What gets printed?

a = 0 if a == 0: a = a + 1 else: a = a + 1 print a

2/13/17 Conditionals & Control Flow 18

prints 1

slide-19
SLIDE 19

What gets printed?

a = 0 if a == 1: a = a + 1 else: a = a + 1 print a

2/13/17 Conditionals & Control Flow 19

prints 1

slide-20
SLIDE 20

What gets printed?

a = 0 if a == 1: a = a + 1 else: a = a + 1 a = a + 1 print a

2/13/17 Conditionals & Control Flow 20

prints 2

slide-21
SLIDE 21

What gets printed?

a = 0 if a == 1: a = a + 1 else: a = a + 1 a = a + 1 a = a + 1 print a

2/13/17 Conditionals & Control Flow 21

prints 3

slide-22
SLIDE 22

Program Flow and Call Frames

  • if can change which statement is executed next

2/13/17 Conditionals & Control Flow 22

foo 1

def foo(a): 1 if a == 0: 2 print “hi” 3 print “bye” foo(0)

a a foo 2

slide-23
SLIDE 23

Program Flow and Call Frames

  • if can change which statement is executed next

2/13/17 Conditionals & Control Flow 23

foo 1

def foo(a): 1 if a == 0: 2 print “hi” 3 print “bye” foo(1)

a 1 a 1 foo 3

slide-24
SLIDE 24

Program Flow and Call Frames

def max(x,y): 1 if x > y: 2 return x 3 return y

max(0,3):

2/13/17 Conditionals & Control Flow 24

max 1 x y 3

slide-25
SLIDE 25

What happens next?

2/13/17 Conditionals & Control Flow 25

max 1 x y 3

C:

max x y 3

RETURN 3

D:

max 3 x y 3

B:

max x y 3

RETURN 0

A:

max 2 x y 3 Current call frame:

def max(x,y): 1 if x > y: 2 return x 3 return y

slide-26
SLIDE 26

Program Flow and Call Frames

max(0,3):

2/13/17 Conditionals & Control Flow 26

max 3 x y 3

Skips line 2 def max(x,y): 1 if x > y: 2 return x 3 return y

slide-27
SLIDE 27

Program Flow and Call Frames

max(0,3):

2/13/17 Conditionals & Control Flow 27

max x y 3

Skips line 2

RETURN

3

def max(x,y): 1 if x > y: 2 return x 3 return y

slide-28
SLIDE 28

Program Flow and Variables

  • Variables continue to exist outside of if
  • Also continue to exist even if created in if

2/13/17 Conditionals & Control Flow 28

a = 0 if a == 0: a = a + 1 print a

slide-29
SLIDE 29

Program Flow and Variables

a = 0 if a == 0: b = 0 print b

2/13/17 Conditionals & Control Flow 29

prints 0

slide-30
SLIDE 30

Program Flow and Variables

a = 0 if a == 1: b = 0 print b

2/13/17 Conditionals & Control Flow 30

Error!

slide-31
SLIDE 31

Program Flow and Variables

def zero_or_one(b): if b: a = 0 else: a = 1 print a

2/13/17 Conditionals & Control Flow 31

better make sure that ALL if branches create the variable

slide-32
SLIDE 32

Program Flow and Testing

  • Can use print statements

to examine program flow 'before if' 'if x>y‘ 'after if' # Put max of x, y in z print 'before if' if x > y: print 'if x>y' z = x else: print 'else x<=y' z = y print 'after if'

Traces x must have been greater than y

2/13/17 Conditionals & Control Flow 32

slide-33
SLIDE 33

Conditionals: If-Elif-Else-Statements Format

if <Boolean expression>: <statement> … elif <Boolean expression>: <statement> … … else: <statement> …

Example

# Put max of x, y, z in w if x > y and x > z: w = x elif y > z: w = y else: w = z

2/13/17 Conditionals & Control Flow 33

slide-34
SLIDE 34

Conditionals: If-Elif-Else-Statements Format

if <Boolean expression>: <statement> … elif <Boolean expression>: <statement> … … else: <statement> …

Notes on Use

2/13/17 Conditionals & Control Flow 34

  • No limit on number of elif
  • Must be between if, else
  • else is optional
  • if-elif by itself is fine
  • Booleans checked in order
  • Once Python finds a true

<Boolean-expression>, skips over all the others

  • else means all are false
slide-35
SLIDE 35

If-Elif-Else

a = 2 if a == 2: a = 3 elif a == 3: a = 4 print a

2/13/17 Conditionals & Control Flow 35

A: 2 B: 3 C: 4 D: I do not know

What gets printed? CORRECT

slide-36
SLIDE 36

If-Elif-Else

a = 2 if a == 2: a = 3 elif a == 3: a = 4 print a

2/13/17 Conditionals & Control Flow 36

a = 2 if a == 2: a = 3 if a == 3: a = 4 print a prints 3 prints 4

slide-37
SLIDE 37

Nested Conditionals

def test_for_zeros(a, b): if a == 0: if b == 0: print "Both arguments are zero" else: print "The first argument is zero" else: if b == 0: print "The second argument is zero" else: print "Neither argument is zero"

2/13/17 Conditionals & Control Flow 37

slide-38
SLIDE 38

Conditional Expressions

Format e1 if bexp else e2

  • e1 and e2 are any expression
  • bexp is a Boolean expression
  • This is an expression!

Example # Put max of x, y in z z = x if x > y else y

2/13/17 Conditionals & Control Flow 38

expression, not statement