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. • 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 2 nd . • Read section 2.3 of A1 carefully to understand how you can revise. 2/13/17 Conditionals & Control Flow 2
Announcements • Please do not post code to Piazza 1 • 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 1 actually violating academic integrity rules because you are showing code to others 2/13/17 Conditionals & Control Flow 3
Methods: Functions Tied to Classes • Method : function tied to class p id3 Method call looks like a function call preceded by a variable name: ⟨ variable ⟩ . ⟨ method ⟩ ( ⟨ arguments ⟩ ) id3 Example : p.distanceTo(q) Point3 5.0 x • Just like we saw for strings s = 'abracadabra' 2.0 y s.index('a') 3.0 z • Are strings objects? Actually, yes . 2/13/17 Conditionals & Control Flow 4
Name Resolution id3 id4 • ⟨ object ⟩ . ⟨ name ⟩ means p q Go the folder for object id3 id4 Point3 Point3 Look for attribute/method name x 5.0 x 7.4 If missing, check class folder y 2.0 y 0.0 • Class folder is a shared folder z 3.0 z 0.0 Only one for the whole class Shared by all objects of class Point3 Stores common features x, y, z Typically where methods are distanceTo(other) abs() 2/13/17 Conditionals & Control Flow 5
Structure vs. Flow Program Structure Control Flow • Order in which statements • Order in which statements are are written in scripts and actually executed at runtime modules Statements may be: • Not necessarily the order in • skipped which Python executes them • executed more than once Have already seen this difference with functions 2/13/17 Conditionals & Control Flow 6
Structure vs. Flow: Example Program Structure Control Flow This statement C:\> python foo.py listed only once def foo(): 'Hello' Statement print 'Hello' 'Hello' executed 3 times 'Hello' # Script Code foo() foo() foo() 2/13/17 Conditionals & Control Flow 7
Conditionals: If-Statements Format Example if < boolean-expression >: # Put x in z if it is positive < statement > if x > 0: … z = x < statement > Execution : if ⟨ Boolean-expression ⟩ is true, then execute all of the statements indented directly underneath (until first non-indented statement) 2/13/17 Conditionals & Control Flow 8
What gets printed? a = 0 prints 0 print a 2/13/17 Conditionals & Control Flow 9
What gets printed? a = 0 a = a + 1 prints 1 print a 2/13/17 Conditionals & Control Flow 10
What gets printed? a = 0 if a == 0: prints 1 a = a + 1 print a 2/13/17 Conditionals & Control Flow 11
What gets printed? a = 0 if a == 1: prints 0 a = a + 1 print a 2/13/17 Conditionals & Control Flow 12
What gets printed? a = 0 if a == 1: prints 1 a = a + 1 a = a + 1 print a 2/13/17 Conditionals & Control Flow 13
What gets printed? a = 0 if a == 0: prints 2 a = a + 1 a = a + 1 print a 2/13/17 Conditionals & Control Flow 14
What gets printed? a = 0 Executed if a == 0: A: 0 a = a + 1 Executed B: 1 if a == 0: C: 2 CORRECT a = a + 1 Skipped D: 3 a = a + 1 Executed E: I do not know print a 2/13/17 Conditionals & Control Flow 15
Conditionals: If-Else-Statements Format Example if < boolean-expression >: # Put max of x and y in z < statement > if x > y: … z = x else : else : < statement > z = y … Execution : if ⟨ Boolean-expression ⟩ is true, then execute statements indented under if ; otherwise execute the statements indented under else 2/13/17 Conditionals & Control Flow 16
Conditionals: “Control Flow” Statements b Branch Point: if b : True Evaluate & Choose False s1 # statement s1 s3 Statement: Execute s3 if b : b False s1 Flow True else : Program only s1 s2 takes one path s2 each execution s3 s3 2/13/17 Conditionals & Control Flow 17
What gets printed? a = 0 if a == 0: prints 1 a = a + 1 else: a = a + 1 print a 2/13/17 Conditionals & Control Flow 18
What gets printed? a = 0 if a == 1: prints 1 a = a + 1 else: a = a + 1 print a 2/13/17 Conditionals & Control Flow 19
What gets printed? a = 0 if a == 1: prints 2 a = a + 1 else: a = a + 1 a = a + 1 print a 2/13/17 Conditionals & Control Flow 20
What gets printed? a = 0 if a == 1: prints 3 a = a + 1 else: a = a + 1 a = a + 1 a = a + 1 print a 2/13/17 Conditionals & Control Flow 21
Program Flow and Call Frames • if can change which statement is executed next def foo(a): foo 1 if a == 0: 1 a 0 print “hi” 2 print “bye” 3 foo(0) foo 2 a 0 2/13/17 Conditionals & Control Flow 22
Program Flow and Call Frames • if can change which statement is executed next def foo(a): foo 1 if a == 0: 1 a 1 print “hi” 2 print “bye” 3 foo(1) foo 3 a 1 2/13/17 Conditionals & Control Flow 23
Program Flow and Call Frames def max(x,y): max(0,3) : if x > y: 1 return x 2 max 1 return y 3 x 0 y 3 2/13/17 Conditionals & Control Flow 24
What happens next? A: B: def max(x,y): max max 2 if x > y: 1 x x 0 0 return x 2 y y 3 3 return y 3 RETURN 0 Current call frame: C: D: max max 3 max 1 x x 0 0 x 0 y y 3 3 y 3 RETURN 3 2/13/17 Conditionals & Control Flow 25
Program Flow and Call Frames def max(x,y): max(0,3) : if x > y: 1 return x 2 max 3 return y 3 x 0 y 3 Skips line 2 2/13/17 Conditionals & Control Flow 26
Program Flow and Call Frames def max(x,y): max(0,3) : if x > y: 1 return x 2 max return y 3 x 0 RETURN y 3 3 Skips line 2 2/13/17 Conditionals & Control Flow 27
Program Flow and Variables • Variables continue to exist outside of if a = 0 if a == 0: a = a + 1 print a • Also continue to exist even if created in if 2/13/17 Conditionals & Control Flow 28
Program Flow and Variables a = 0 if a == 0: prints 0 b = 0 print b 2/13/17 Conditionals & Control Flow 29
Program Flow and Variables a = 0 if a == 1: Error! b = 0 print b 2/13/17 Conditionals & Control Flow 30
Program Flow and Variables def zero_or_one(b): if b: a = 0 better make sure that ALL if else: branches create the variable a = 1 print a 2/13/17 Conditionals & Control Flow 31
Program Flow and Testing • Can use print statements # Put max of x, y in z to examine program flow print 'before if' if x > y: 'before if' print 'if x>y' 'if x>y‘ z = x Traces 'after if' else : print 'else x<=y' z = y x must have been print 'after if' greater than y 2/13/17 Conditionals & Control Flow 32
Conditionals: If-Elif-Else-Statements Format Example if < Boolean expression >: # Put max of x, y, z in w < statement > if x > y and x > z: … w = x elif < Boolean expression >: elif y > z: < statement > … w = y … else : else : w = z < statement > … 2/13/17 Conditionals & Control Flow 33
Conditionals: If-Elif-Else-Statements Format Notes on Use if < Boolean expression >: • No limit on number of elif < statement > Must be between if , else … • else is optional elif < Boolean expression >: if - elif by itself is fine < statement > … • Booleans checked in order Once Python finds a true … < Boolean-expression > , else : skips over all the others < statement > else means all are false … 2/13/17 Conditionals & Control Flow 34
If-Elif-Else a = 2 What gets printed? if a == 2: A: 2 a = 3 B: 3 CORRECT elif a == 3: C: 4 a = 4 D: I do not know print a 2/13/17 Conditionals & Control Flow 35
If-Elif-Else a = 2 a = 2 if a == 2: if a == 2: a = 3 a = 3 elif a == 3: if a == 3: a = 4 a = 4 print a print a prints 4 prints 3 2/13/17 Conditionals & Control Flow 36
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
Conditional Expressions Format Example # Put max of x, y in z e 1 if bexp else e 2 z = x if x > y else y • e 1 and e 2 are any expression • bexp is a Boolean expression • This is an expression! expression, not statement 2/13/17 Conditionals & Control Flow 38
Recommend
More recommend