Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Similar documents
Conditionals & Control Flow

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Lecture 11: Iteration and For-Loops

Lecture 9: Memory in Python

Lecture 8. Conditionals & Control Flow

Lecture 10: Lists and Sequences

PREPARING FOR PRELIM 1

CS Boolean Statements and Decision Structures. Week 6

CS177 Recitation. Functions, Booleans, Decision Structures, and Loop Structures

Lecture 4: Defining Functions

CSE 115. Introduction to Computer Science I

Iteration and For Loops

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 17: Classes (Chapter 15)

Lecture 24: Loop Invariants

Control Flow: Branching booleans and selection statements CS GMU

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 6: Specifications & Testing

Lecture 20: While Loops (Sections 7.3, 7.4)

Lecture 2: Variables & Assignments

CS 1110: Introduction to Computing Using Python Loop Invariants

CS 1110 Prelim 1 March 15th, 2016

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Lecture 3: Functions & Modules

CS Lecture 19: Loop invariants

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

CS 1110 Prelim 1 March 10, 2015

Lecture 3: Functions & Modules (Sections ) CS 1110 Introduction to Computing Using Python

CMSC201 Computer Science I for Majors

LECTURE 04 MAKING DECISIONS

Practice with if-then-else. COMP101 Lecture 7 Spring 2018

CS Lecture 26: Subclasses in Event-driven Programs A7 is out! Get started right away you need time to ask questions.

Chapter 4: Making Decisions

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Chapter 4: Making Decisions

Control and Environments Fall 2017 Discussion 1: August 30, Control. If statements. Boolean Operators

CS 61A Control and Environments Spring 2018 Discussion 1: January 24, Control. If statements. Boolean Operators

isinstance and While Loops

Python Activity 5: Boolean Expressions and Selection Statements

CS 1110, LAB 3: STRINGS; TESTING

C ONTROL AND H IGHER O RDER F UNCTIONS

Lecture 4 8/24/18. Expressing Procedural Knowledge. Procedural Knowledge. What are we going to cover today? Computational Constructs

Software Development Techniques. December Sample Examination Paper. Time: 3 hours

Lecture 17: Classes (Chapter 15)

61A Lecture 3. Friday, September 5

Lecture 5. Defining Functions

CS1 Lecture 15 Feb. 18, 2019

Control and Environments Fall 2017 Discussion 1: August 30, 2017 Solutions. 1 Control. If statements. Boolean Operators

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

CONTROL AND ENVIRONMENTS 1

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

Computer Science 217

CONTROL AND HIGHER ORDER FUNCTIONS 1

Sorting and Searching

CS Lecture 25: Models, Views, Controllers, and Games. Announcements

Lecture 4. Conditionals and Boolean Expressions

boolean & if-then-else

Introduction to Programming

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Lecture #7: Recursion (and a data structure)

Conditionals and Recursion. Python Part 4

Lecture 18: Using Classes Effectively (Chapter 16)

How Do Robots Find Their Way?

61A Lecture 21. Friday, March 13

Visualize ComplexCities

CS Prelim 1 Review Fall 2018

boolean Expressions Relational and Equality Operators and if-then-else Conditional Control Statement

CS1110. Lecture 6: Function calls

CS 1110, LAB 03: STRINGS; TESTING First Name: Last Name: NetID:

npm run pull npm start

CS 1110, Spring 2018: Prelim 1 study guide Prepared Tuesday March 6, 2018

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

CS1110. Lecture 6: Function calls

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

Python Programming: An Introduction to Computer Science

[301] Objects/References. Tyler Caraza-Harter

CS Lecture 26: Grab Bag. Announcements

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

The L Line. The Express Line to Learning. Wiley Publishing All Rights Reserved.

Chapter 5 Conditional and Iterative Statements. Statement are the instructions given to the computer to perform any kind of action.

Exam 2, Form A CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Exam 2, Form B CSE 231 Spring 2014 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

FUNctions. Lecture 03 Spring 2018

Classes, Types, and Objects

CS1 Lecture 5 Jan. 26, 2018

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

CS 1110, LAB 13: SEQUENCE ALGORITHMS

Python - Conditional Execution

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

CS Prelim 1 Review Fall 2017

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

Announcements. Project 2 due next Monday. Next Tuesday is review session; Midterm 1 on Wed., EE 129, 8:00 9:30pm

Flow Control. So Far: Writing simple statements that get executed one after another.

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

15-110: Principles of Computing Sample Exam #1

Software Development Techniques. December Sample Exam Marking Scheme

Lecture 5: Strings

CS 301: Recursion. The Art of Self Reference. Tyler Caraza-Harter

Transcription:

http://www.cs.cornell.edu/courses/cs1110/2018sp Lecture 8: Conditionals & Control Flow (Sections 5.1-5.7) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

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

Big Picture Statements either affect data or control DATA: change the value in a box, create a box, etc. Examples: x = x + 1 name = Alex CONTROL: tell python what line to execute next Examples: greet(name) if name == Alex : ß today s Lecture 3

Conditionals: If-Statements Format if <boolean-expression>: <statement> <statement> Example # is there a new high score? if curr_score > high_score: high_score = curr_score print( New high score! ) Execution: if Boolean-expression is true, then execute all of the statements indented directly underneath (until first non-indented statement) 4

What are Boolean expressions? Expressions that evaluate to a Boolean value. is_student = True is_senior = False num_credits = 25 Boolean operations: if is_student and is_senior: print( Hi senior student! ) Boolean variables: if is_student: print( Hi student! ) Comparison operations: if num_credits > 24: print( Are you serious? ) 5

What gets printed, Round 1 a = 0 a = 0 a = 0 a = 0 a = 0 print(a) a = a + 1 if a == 0: if a == 1: if a == 0: print(a) a = a + 1 a = a + 1 a = a + 1 print(a) print(a) a = a + 1 print(a) 0 1 1 0 2 6

What gets printed? (Question) a = 0 if a == 0: a = a + 1 if a == 0: a = a + 2 a = a + 1 A: 0 B: 1 C: 2 D: 3 E: I do not know print(a) 7

What gets printed? (Solution) a = 0 if a == 0: a = a + 1 if a == 0: a = a + 2 a = a + 1 Executed Executed Executed Executed Skipped Executed A: 0 B: 1 C: 2 CORRECT D: 3 E: I do not know print(a) 8

Conditionals: If-Else-Statements Format if <boolean-expression>: <statement> else: <statement> Example # who is the winner? if score1 > score2: winner = Player 1 else: winner = Player 2 Execution: if Boolean-expression is true, then execute statements indented under if; otherwise execute the statements indented under else 9

Conditionals: Control Flow Statements if b : True b Branch Point: Evaluate & Choose s3 s1 # statement # statement s1 s3 False Statements: Execute if b : s1 else: s2 s1 b True False s2 Flow Program only takes one path each execution s3 s3 10

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 if b : s1 else: s2 s3 s1 b True s3 b s3 False s2 11

What gets printed, Round 2 a = 0 a = 0 a = 0 a = 0 if a == 0: if a == 1: if a == 1: if a == 1: a = a + 1 a = a + 1 a = a + 1 a = a + 1 else: else: else: else: a = a + 2 a = a + 2 a = a + 2 a = a + 1 a = a + 1 a = a + 1 print(a) print(a) print(a) a = a + 1 1 2 3 print(a) 3 12

Program Flow (car locked, step 1) if determines which statement is executed next def get_in_car(car_locked): 1 if car_locked: 2 print( Unlock the car! ) 3 print( Open the door. ) get_in_car 1 car_locked True is_my_car_locked = True get_in_car(is_my_car_locked) 13

Program Flow (car locked, step 2) if determines which statement is executed next def get_in_car(car_locked): 1 if car_locked: 2 print( Unlock the car! ) 3 print( Open the door. ) get_in_car 2 car_locked True is_my_car_locked = True get_in_car(is_my_car_locked) 14

Program Flow (car not locked, step 1) if determines which statement is executed next def get_in_car(car_locked): 1 if car_locked: 2 print( Unlock the car! ) 3 print( Open the door. ) get_in_car 1 car_locked False is_my_car_locked = False get_in_car(is_my_car_locked) 15

Program Flow (car not locked, step 2) if determines which statement is executed next def get_in_car(car_locked): 1 if car_locked: 2 print( Unlock the car! ) 3 print( Open the door. ) get_in_car 3 car_locked False is_my_car_locked = False get_in_car(is_my_car_locked) 16

What does the call frame look like next? (Q) def max(x,y): 1 if x > y: 2 return x A: max 2 x 0 y 3 B: max x 0 y 3 3 return y max(0,3) Current call frame: max 1 C: max x 0 RETURN 0 D: max 3 x 0 x 0 y 3 y 3 RETURN 3 y 3 17

What does the call frame look like next? (A) def max(x,y): 1 if x > y: 2 return x A: max 2 x 0 y 3 B: max x 0 y 3 3 return y max(0,3) Current call frame: max 1 x 0 y 3 C: max x 0 y 3 RETURN 3 RETURN 0 D: max 3 x 0 y 3 18

Call Frame Explanation (1) def max(x,y): 1 if x > y: 2 return x 3 return y max(0,3): max 1 x 0 y 3 19

Call Frame Explanation (2) def max(x,y): 1 if x > y: 2 return x 3 return y max(0,3): max 3 x 0 y 3 Skips line 2 20

Call Frame Explanation (3) def max(x,y): 1 if x > y: 2 return x 3 return y max(0,3): max x 0 y 3 RETURN 3 21

Program Flow and Variables Variables created inside if continue to exist past if: a = 0 if a == 0: b = a + 1 print(b) but are only created if the program actually executes that line of code 22

What gets printed, Round 3 a = 0 if a == 0: b = 0 print(b) a = 0 if a == 1: b = 0 print(b) 0 Error! 23

Program Flow and Variables def zero_or_one(a): if a == 1: b = 1 else: b = 0 print(b) make sure that ALL if branches create the variable 24

Control Flow and Variables (Q1) def max(x,y): """Returns: max of x, y""" # note: code has a bug! # check if x is larger if x > y: bigger = x return bigger Value of maximum? A: 3 B: 0 C: Error! D: I do not know maximum = max(3,0) 25

Control Flow and Variables (A1) def max(x,y): """Returns: max of x, y""" # note: code has a bug! # check if x is larger if x > y: bigger = x return bigger maximum = max(3,0) Value of maximum? A: 3 CORRECT B: 0 C: Error! D: I do not know Local variables last until They are deleted or End of the function Even if defined inside if 26

Control Flow and Variables (Q2) def max(x,y): """Returns: max of x, y""" # note: code has a bug! # check if x is larger if x > y: bigger = x return bigger Value of maximum? A: 3 B: 0 C: Error! D: I do not know maximum = max(0,3) 27

Control Flow and Variables (A2) def max(x,y): """Returns: max of x, y""" # note: code has a bug! # check if x is larger if x > y: bigger = x return bigger maximum = max(0,3) Value of maximum? A: 3 B: 0 C: Error! CORRECT D: I do not know Variable existence depends on flow Generally terrible idea to refer to variables defined inside an if clause 28

Program Flow and Testing Can use print statements to examine program flow 'before if inside if x>y 'after if' x must have been greater than y # Put max of x, y in z print('before if ) if x > y: print( inside if x>y ) z = x else: print( inside else (x<=y) ) z = y print('after if ) breadcrumbs 29

Conditionals: If-Elif-Else-Statements Format if <Boolean expression>: <statement> elif <Boolean expression>: <statement> else: <statement> Example # Find the winner if score1 > score2: winner = Player 1 elif score2 > score1: winner = Player 2 else: winner = Both Players" 30

Conditionals: If-Elif-Else-Statements Format if <Boolean expression>: <statement> elif <Boolean expression>: <statement> else: <statement> Notes on Use 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 31

If-Elif-Else (Question) a = 2 if a == 2: a = 3 elif a == 3: a = 4 print(a) What gets printed? A: 2 B: 3 C: 4 D: I do not know 32

If-Elif-Else (Answer) a = 2 if a == 2: a = 3 elif a == 3: a = 4 print(a) What gets printed? A: 2 B: 3 CORRECT C: 4 D: I do not know 33

What gets printed, Round 4 a = 2 a = 2 if a == 2: a = 3 elif a == 3: a = 4 print(a) 3 if a == 2: a = 3 if a == 3: a = 4 print(a) 4 34

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.") dude_wheres_my_prelim.py 35