Conditionals & Control Flow

Similar documents
Lecture 8. Conditionals & Control Flow

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

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

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Lecture 4: Defining Functions

CS1110. Lecture 6: Function calls

Announcements for this Lecture

Lecture 17: Classes (Chapter 15)

PREPARING FOR PRELIM 1

Lecture 10: Lists and Sequences

Lecture 5: Strings

CS 1110: Introduction to Computing Using Python Loop Invariants

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

Lecture 19: Subclasses & Inheritance (Chapter 18)

Announcements For This Lecture

CS1 Lecture 5 Jan. 26, 2018

Lecture 2: Variables & Assignments

CS 1110, LAB 3: STRINGS; TESTING

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

Lecture 20: While Loops (Sections 7.3, 7.4)

Lecture 9: Memory in Python

Lecture 9. Memory and Call Stacks

Announcements For This Lecture

Lecture 11: Iteration and For-Loops

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

CS1110. Lecture 6: Function calls

61A Lecture 3. Friday, September 5

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

CS1 Lecture 5 Jan. 25, 2019

Lecture 5. Defining Functions

isinstance and While Loops

Lecture 17: Classes (Chapter 15)

Part 1 (80 points) Multiple Choice Questions (20 questions * 4 points per question = 80 points)

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

CS Lecture 19: Loop invariants

Iteration and For Loops

Lecture 18: Using Classes Effectively (Chapter 16)

CSE 115. Introduction to Computer Science I

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

Lecture 3: Functions & Modules

Lecture 24: Loop Invariants

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

Lecture 7. Memory in Python

Lecture 4. Defining Functions

CS Introduction to Programming Fall 2016

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

CS 112: Intro to Comp Prog

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

Programming for Engineers in Python. Autumn

Lecture 4. Defining Functions

CS Lecture 26: Grab Bag. Announcements

Lecture 6: Specifications & Testing

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

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

Selection statements. CSE 1310 Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington

Programming for Engineers in Python. Recitation 1

Visualize ComplexCities

CME 193: Introduction to Scientific Python Lecture 1: Introduction

Lecture 19. Operators and Abstraction

CS 1110, LAB 2: FUNCTIONS AND ASSIGNMENTS

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Lecture 10. Overriding & Casting About

61A LECTURE 1 FUNCTIONS, VALUES. Steven Tang and Eric Tzeng June 24, 2013

CS1 Lecture 3 Jan. 18, 2019

Introduction to Python

Data 8 Final Review #1

Python Basics. Lecture and Lab 5 Day Course. Python Basics

EECE.2160: ECE Application Programming Spring 2017

15-110: Principles of Computing, Spring Problem Set 2 (PS2) Due: Friday, February 2 by 2:30PM on Gradescope

CS1 Lecture 3 Jan. 22, 2018

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

CS1 Lecture 13 Feb. 13, 2019

Course Overview, Python Basics

Basic Syntax - First Program 1

Lecture 4. Defining Functions

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

Conditionals and Recursion. Python Part 4

CSCE 206: Structured Programming in C++

CS1110. Lecture 1: Final review session. Review materials See website for a version of last year s final with conventions redone to match this year.

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

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Accelerating Information Technology Innovation

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

Lecture no

PREPARING FOR THE FINAL EXAM

Sorting and Searching

Announcements for This Lecture

Lecture 1. Course Overview, Python Basics

Module 01: Introduction to Programming in Python

Control Flow: Branching booleans and selection statements CS GMU

CSCI544, Fall 2016: Assignment 2

CIS192 Python Programming. Robert Rand. August 27, 2015

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

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

CS Boolean Statements and Decision Structures. Week 6

15-110: Principles of Computing, Spring 2018

Lecture 10. Memory in Python

Transcription:

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 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') p id3 x 5.0 y 2.0 z 3.0 id3 Point3 Are strings objects? Actually, yes. 2/13/17 Conditionals & Control Flow 4

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 p id3 x 5.0 y 2.0 z 3.0 id3 Point3 q id4 x 7.4 y 0.0 z 0.0 Point3 x, y, z distanceto(other) abs() id4 Point3 2/13/17 Conditionals & Control Flow 5

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

Structure vs. Flow: Example Program Structure def foo(): print 'Hello' # Script Code foo() foo() foo() This statement listed only once Control Flow C:\> python foo.py 'Hello' 'Hello' 'Hello' Statement executed 3 times 2/13/17 Conditionals & Control Flow 7

Conditionals: If-Statements Format if <boolean-expression>: <statement> <statement> Example # Put x in z if it is positive if x > 0: z = x 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 prints 1 print a 2/13/17 Conditionals & Control Flow 10

What gets printed? a = 0 if a == 0: prints 1 print a 2/13/17 Conditionals & Control Flow 11

What gets printed? a = 0 if a == 1: prints 0 print a 2/13/17 Conditionals & Control Flow 12

What gets printed? a = 0 if a == 1: prints 1 print a 2/13/17 Conditionals & Control Flow 13

What gets printed? a = 0 if a == 0: prints 2 print a 2/13/17 Conditionals & Control Flow 14

What gets printed? a = 0 if a == 0: if a == 0: Executed Executed Skipped Executed A: 0 B: 1 C: 2 CORRECT D: 3 E: I do not know print a 2/13/17 Conditionals & Control Flow 15

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 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 if b : True b Branch Point: Evaluate & Choose s1 # statement s1 False s3 s3 Statement: Execute if b : s1 else: s2 s3 s1 b True s3 False s2 Flow Program only takes one path each execution 2/13/17 Conditionals & Control Flow 17

What gets printed? a = 0 if a == 0: else: prints 1 print a 2/13/17 Conditionals & Control Flow 18

What gets printed? a = 0 if a == 1: else: prints 1 print a 2/13/17 Conditionals & Control Flow 19

What gets printed? a = 0 if a == 1: else: print a prints 2 2/13/17 Conditionals & Control Flow 20

What gets printed? a = 0 if a == 1: else: print a prints 3 2/13/17 Conditionals & Control Flow 21

Program Flow and Call Frames if can change which statement is executed next def foo(a): 1 if a == 0: 2 print hi 3 print bye foo(0) foo 1 a 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): 1 if a == 0: 2 print hi 3 print bye foo(1) foo 1 a 1 foo 3 a 1 2/13/17 Conditionals & Control Flow 23

Program Flow and Call Frames def max(x,y): 1 if x > y: 2 return x 3 return y max(0,3): max 1 x 0 y 3 2/13/17 Conditionals & Control Flow 24

What happens next? 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 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 2/13/17 Conditionals & Control Flow 25

Program Flow and Call Frames 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 2/13/17 Conditionals & Control Flow 26

Program Flow and Call Frames def max(x,y): 1 if x > y: 2 return x 3 return y max(0,3): max x 0 y 3 RETURN 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: 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: b = 0 print b prints 0 2/13/17 Conditionals & Control Flow 29

Program Flow and Variables a = 0 if a == 1: b = 0 print b Error! 2/13/17 Conditionals & Control Flow 30

Program Flow and Variables def zero_or_one(b): if b: else: a = 0 a = 1 print a better make sure that ALL if branches create the variable 2/13/17 Conditionals & Control Flow 31

Program Flow and Testing Can use print statements to examine program flow 'before if' '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 'if x>y' z = x else: print 'else x<=y' z = y print 'after if' Traces 2/13/17 Conditionals & Control Flow 32

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

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 2/13/17 Conditionals & Control Flow 34

If-Elif-Else 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 2/13/17 Conditionals & Control Flow 35

If-Elif-Else a = 2 a = 2 if a == 2: a = 3 elif a == 3: a = 4 print a if a == 2: a = 3 if a == 3: a = 4 print a prints 3 prints 4 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 e 1 if bexp else e 2 e 1 and e 2 are any expression Example # Put max of x, y in z z = x if x > y else y bexp is a Boolean expression This is an expression! expression, not statement 2/13/17 Conditionals & Control Flow 38