Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow...
Transcript of Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow...
![Page 1: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/1.jpg)
Lecture 8: Conditionals & Control Flow
(Sections 5.1-5.7)
CS 1110Introduction to Computing Using Python
[E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
http://www.cs.cornell.edu/courses/cs1110/2018sp
![Page 2: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/2.jpg)
Feb 22: CS 1110: Announcements
• Sign up for a one-on-one! § CMS: OPTIONAL: one-on-ones
• Prelim 1 is March 13. You have until March 1st, 11:59pm to register a conflict or a need for accommodation. There is no single makeup session. See website: “Assessment à Exams”§ CMS: Prelim 1 conflicts
• A2 tentatively: released Wed 2/28, due Wed 3/7
![Page 3: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/3.jpg)
Big Picture
Statements either affect data or control• DATA: change the value in a box, create a box, etc.
Examples:x = x + 1name = “Alex”
• CONTROL: tell python what line to execute nextExamples:
greet(name)if name == ”Alex”: ß today’s Lecture
3
![Page 4: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/4.jpg)
Conditionals: If-Statements
Formatif <boolean-expression>:
<statement>…<statement>
Example# is there a new high score? if curr_score > high_score:
high_score = curr_scoreprint(“New high score!”)
4
Execution:
if ⟨Boolean-expression⟩ is true, then execute all of the statements
indented directly underneath (until first non-indented statement)
![Page 5: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/5.jpg)
What are Boolean expressions?
Boolean operations:if is_student and is_senior:
print(“Hi senior student!”)
Comparison operations:if num_credits > 24:
print(“Are you serious?”)
is_student = Trueis_senior = Falsenum_credits = 25
Boolean variables:if is_student:
print(“Hi student!”)
5
Expressions that evaluate to a Boolean value.
![Page 6: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/6.jpg)
What gets printed, Round 1
a = 0print(a)
6
0
a = 0a = a + 1print(a)
1
a = 0if a == 0:
a = a + 1print(a)
1
a = 0if a == 1:
a = a + 1print(a)
0
a = 0if a == 0:
a = a + 1a = a + 1print(a)
2
![Page 7: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/7.jpg)
What gets printed? (Question)
a = 0if a == 0:
a = a + 1if a == 0:
a = a + 2a = a + 1
print(a)
7
A: 0B: 1C: 2D: 3E: I do not know
![Page 8: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/8.jpg)
What gets printed? (Solution)
a = 0if a == 0:
a = a + 1if a == 0:
a = a + 2a = a + 1
print(a)
8
A: 0B: 1C: 2D: 3E: I do not know
CORRECT
Executed
Executed
Skipped
Executed
Executed
Executed
![Page 9: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/9.jpg)
Conditionals: If-Else-Statements
Formatif <boolean-expression>:
<statement>…
else:<statement>…
Example# who is the winner?if score1 > score2:
winner = “Player 1”else:
winner = “Player 2”
9
Execution:
if ⟨Boolean-expression⟩ is true, then execute statements indented
under if; otherwise execute the statements indented under else
![Page 10: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/10.jpg)
Conditionals: “Control Flow” Statements
if b : s1 # statement
s3 # statement
if b :s1
else:s2
s310
b
Statements:Execute
b Branch Point:Evaluate & Choose
s3
s3
FlowProgram only takes one path each execution
s1
True
False
s1True
s2
False
![Page 11: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/11.jpg)
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
11
if b :s1
else:s2
s3
b
s3
s1True
b
s3
s2
False
![Page 12: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/12.jpg)
What gets printed, Round 2
a = 0if a == 0:
a = a + 1else:
a = a + 2
print(a)
12
1
a = 0if a == 1:
a = a + 1else:
a = a + 2
print(a)
2
a = 0if a == 1:
a = a + 1else:
a = a + 2a = a + 1print(a)
3
a = 0if a == 1:
a = a + 1else:
a = a + 1a = a + 1
a = a + 1print(a)3
![Page 13: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/13.jpg)
Program Flow (car locked, step 1)
if determines which statement is executed next
13
get_in_car 1def get_in_car(car_locked):1 if car_locked:2 print(“Unlock the car!”)3 print(“Open the door.”)
is_my_car_locked = Trueget_in_car(is_my_car_locked)
car_locked True
![Page 14: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/14.jpg)
Program Flow (car locked, step 2)
if determines which statement is executed next
14
get_in_car 2
True
def get_in_car(car_locked):1 if car_locked:2 print(“Unlock the car!”)3 print(“Open the door.”)
is_my_car_locked = Trueget_in_car(is_my_car_locked)
car_locked
![Page 15: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/15.jpg)
Program Flow (car not locked, step 1)
if determines which statement is executed next
15
get_in_car 1
False
def get_in_car(car_locked):1 if car_locked:2 print(“Unlock the car!”)3 print(“Open the door.”)
is_my_car_locked = Falseget_in_car(is_my_car_locked)
car_locked
![Page 16: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/16.jpg)
Program Flow (car not locked, step 2)
if determines which statement is executed next
16
get_in_car 3
False
def get_in_car(car_locked):1 if car_locked:2 print(“Unlock the car!”)3 print(“Open the door.”)
is_my_car_locked = Falseget_in_car(is_my_car_locked)
car_locked
![Page 17: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/17.jpg)
What does the call frame look like next? (Q)
17
max 1
x 0
y 3
C: maxx 0
y 3
RETURN 3
D: max 3
x 0
y 3
B: maxx 0
y 3
RETURN 0
A: max 2
x 0
y 3
Current call frame:
def max(x,y):1 if x > y:2 return x3 return y
max(0,3)
![Page 18: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/18.jpg)
What does the call frame look like next? (A)
18
max 1
x 0
y 3
C: maxx 0
y 3
RETURN 3
D: max 3
x 0
y 3
B: maxx 0
y 3
RETURN 0
A: max 2
x 0
y 3
Current call frame:
def max(x,y):1 if x > y:2 return x3 return y
max(0,3)
�
![Page 19: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/19.jpg)
Call Frame Explanation (1)
max(0,3):
19
max 1
x 0
y 3
def max(x,y):1 if x > y:2 return x3 return y
![Page 20: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/20.jpg)
Call Frame Explanation (2)
max(0,3):
20
max 3
x 0
y 3
Skips line 2
def max(x,y):1 if x > y:2 return x3 return y
![Page 21: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/21.jpg)
Call Frame Explanation (3)
max(0,3):
21
max
x 0
y 3RETURN3
def max(x,y):1 if x > y:2 return x3 return y
![Page 22: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/22.jpg)
Program Flow and Variables
Variables created inside if continue to exist past if:
…but are only created if the program actually executes that line of code
22
a = 0if a == 0:
b = a + 1print(b)
![Page 23: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/23.jpg)
What gets printed, Round 3
a = 0if a == 0:
b = 0print(b)
23
0
a = 0if a == 1:
b = 0print(b)
Error!
![Page 24: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/24.jpg)
Program Flow and Variables
def zero_or_one(a):if a == 1:
b = 1else:
b = 0print(b)
24
make sure that ALL if branches create
the variable
![Page 25: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/25.jpg)
Control Flow and Variables (Q1)
def max(x,y):"""Returns: max of x, y""" # note: code has a bug!# check if x is largerif x > y:
bigger = xreturn bigger
maximum = max(3,0)
Value of maximum?
25
A: 3B: 0C: Error!D: I do not know
![Page 26: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/26.jpg)
Control Flow and Variables (A1)
Value of maximum?
26
A: 3B: 0C: Error!D: I do not know
CORRECT
• Local variables last until§ They are deleted or§ End of the function
• Even if defined inside if
def max(x,y):"""Returns: max of x, y""" # note: code has a bug!# check if x is largerif x > y:
bigger = xreturn bigger
maximum = max(3,0)
![Page 27: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/27.jpg)
Control Flow and Variables (Q2)
Value of maximum?
27
A: 3B: 0C: Error!D: I do not know
def max(x,y):"""Returns: max of x, y""" # note: code has a bug!# check if x is largerif x > y:
bigger = xreturn bigger
maximum = max(0,3)
![Page 28: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/28.jpg)
Control Flow and Variables (A2)
Value of maximum?
28
A: 3B: 0C: Error!D: I do not know
CORRECT
• Variable existence depends on flow
• Generally terrible idea to refer to variables defined inside an ifclause
def max(x,y):"""Returns: max of x, y""" # note: code has a bug!# check if x is largerif x > y:
bigger = xreturn bigger
maximum = max(0,3)
![Page 29: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/29.jpg)
Program Flow and Testing
Can use print statements to examine program flow
'before if’‘inside if x>y‘'after if'
# Put max of x, y in zprint('before if’)if x > y:
print(‘inside if x>y’)z = x
else:print(‘inside else (x<=y)’)z = y
print('after if’)
“breadcrumbs”
x must have been greater
than y
29
![Page 30: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/30.jpg)
Conditionals: If-Elif-Else-Statements
Formatif <Boolean expression>:
<statement>…
elif <Boolean expression>: <statement>…
…else:
<statement>…
Example
# Find the winnerif score1 > score2:
winner = “Player 1”elif score2 > score1:
winner = “Player 2”else:
winner = “Both Players"
30
![Page 31: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/31.jpg)
Conditionals: If-Elif-Else-Statements
Formatif <Boolean expression>:
<statement>…
elif <Boolean expression>: <statement>…
…else:
<statement>…
Notes on Use
31
• 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
![Page 32: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/32.jpg)
If-Elif-Else (Question)
a = 2
if a == 2:a = 3
elif a == 3:a = 4
print(a)
32
A: 2B: 3C: 4D: I do not know
What gets printed?
![Page 33: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/33.jpg)
If-Elif-Else (Answer)
a = 2
if a == 2:a = 3
elif a == 3:a = 4
print(a)
33
A: 2B: 3C: 4D: I do not know
What gets printed?
CORRECT
![Page 34: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/34.jpg)
What gets printed, Round 4
a = 2
if a == 2:a = 3
elif a == 3:a = 4
print(a)
34
3
a = 2
if a == 2:a = 3
if a == 3:a = 4
print(a)4
![Page 35: Lecture 8: Conditionals & Control Flow - cs.cornell.edu · Lecture 8: Conditionals & Control Flow ... Feb 22:CS 1110: Announcements ... What does the call frame look like next? (Q)](https://reader034.fdocuments.us/reader034/viewer/2022051800/5ac5719b7f8b9a220b8d578f/html5/thumbnails/35.jpg)
Nested Conditionals
def what_to_wear(raining, freezing):if raining:
if freezing:print(”Wear a waterproof coat.”)
else:print(”Bring an umbrella.")
else:if freezing:
print(”Wear a warm coat!")else:
print(”A jacket will suffice.")35
dude_wheres_my_prelim.py