Michael Ernst UW CSE 190p Summer 2012

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Computer Science 1620 Loops.
Geography 465 Assignments, Conditionals, and Loops.
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
If statements while loop for loop
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Control flow Ruth Anderson UW CSE 160 Spring
COMP Loop Statements Yi Hong May 21, 2015.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control flow Ruth Anderson UW CSE 160 Winter
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
The for Statement A most versatile loop
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Lesson #5 Repetition and Loops.
Control Flow (Python) Dr. José M. Reyes Álamo.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Repetition (While-Loop) version]
The switch Statement, and Introduction to Looping
Topics Introduction to Repetition Structures
Python: Control Structures
Lesson #5 Repetition and Loops.
Introduction to Python and programming
Computer Programming Fundamentals
IF statements.
Lecture 4: Program Control Flow
CiS 260: App Dev I Chapter 4: Control Structures II.
Topics Introduction to Repetition Structures
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Logical Operators and While Loops
Looping.
Arrays, For loop While loop Do while loop
Introduction to Python and programming
Selection CIS 40 – Introduction to Programming in Python
Outline Altering flow of control Boolean expressions
Lesson #5 Repetition and Loops.
Introduction to Python and programming
Multiple Selections (ELIF Statements)
Ruth Anderson UW CSE 140 Winter 2014
Topic 1: Problem Solving
Michael Ernst UW CSE 140 Winter 2013
Control flow : if statements
Introduction to Python and programming
Statement-Level Control Structures
Flowcharts and Pseudo Code
Chapter8: Statement-Level Control Structures April 9, 2019
Logical Operators and While Loops
Loop Construct.
Java Programming Loops
Topics Introduction to Repetition Structures
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Lesson #5 Repetition and Loops.
Review of Previous Lesson
EE 194/BIO 196: Modeling biological systems
Controlling Program Flow
Introduction to Python and programming
Programming Fundamental
REPETITION Why Repetition?
Control Structures.
Control flow: Loops UW CSE 160.
Flow Control I Branching and Looping.
Control 9 / 25 / 19.
Presentation transcript:

Michael Ernst UW CSE 190p Summer 2012 Control flow Michael Ernst UW CSE 190p Summer 2012

Repeating yourself Making decisions

Temperature conversion chart Recall exercise from previous lecture fahr = 30 cent = (f-32)/9.0*5 print fahr, cent fahr = 40 fahr = 50 fahr = 60 fahr = 70 print “All done” Output: 30 -1.11 40 4.44 50 10.0 60 15.56 70 21.11 All done

Temperature conversion chart Revisit exercise from previous lecture loop variable or iteration variable for loop A list Loop body is indented for f in [30,40,50,60,70]: print f, (f-32)/9.0*5 print “All done” Execute the body 5 times: once with f = 30 once with f = 40 … Output: 30 -1.11 40 4.44 50 10.0 60 15.56 70 21.11 All done Indentation is significant

The body can be multiple statements Output: Start body 3 9 4 16 5 25 NOT: Start body 3 4 5 9 16 25 Execute whole body, then execute whole body again, etc. for i in [3,4,5]: print “Start body” print i print i*i for i in [0,1]: print “Outer”, i for j in [2,3]: print “ Inner”, j print “ Sum”, i+j Convention: often use i or j as loop variable This is an exception to the rule that variable names should be descriptive loop body: 3 statements Output: Outer 0 Inner 2 Sum 2 Inner 3 Sum 3 Outer 1 Sum 4 loop body: 3 statements “nested” loop body: 2 statements

Indentation is significant Every statement in the body must have exactly the same indentation for i in [3,4,5]: print “Start body” print i print i*I Compare the results of these loops: for f in [30,40,50,60,70]: print f, (f-32)/9.0*5 print “All done” “All done” Error! You can tell what this means, but the computer cannot

Fix this loop What does it actually print? # Goal: print 1, 2, 3, …, 48, 49, 50 for tens_digit in [0, 1, 2, 3, 4]: for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]: print tens_digit * 10 + ones_digit What does it actually print? How can we change it to correct its output? Moral: Watch out for edge conditions (beginning or end of loop) There are multiple ways to correct it.

How a loop is executed (2 versions) Transformation approach: Direct approach: Evaluate sequence expression Write an assignment to the loop variable for each sequence element Write a copy of the loop after each assignment Execute the resulting statements Evaluate sequence expression While there are sequence elements left: Assign the loop variable to the first remaining sequence element Execute the loop body for i in [1,2,3]: print i i = 1 print i i = 2 i = 3

Another example of the transformation approach Key idea: Assign each sequence element to the loop variable Duplicate the body for i in [0,1]: print “Outer”, i for j in [2,3]: print “ Inner”, j i = 0 print “Outer”, i for j in [2,3]: print “ Inner”, j i = 1 i = 0 print “Outer”, i j = 2 print “ Inner”, j j = 3 i = 1 for j in [2,3]:

Test your understanding of loops Output: Puzzle 1: for i in [0,1]: print i Puzzle 2: i = 5 for i in []: Puzzle 3: print “Outer”, i for i in [2,3]: print “ Inner”, i 1 (no output) Reusing loop variable (don’t do this) Outer 0 Inner 2 Inner 3 Outer 3 Outer 1 outer loop body inner loop body

The range function A typical for loop does not use an explicit list: for i in range(5): … body … range(5) = [0,1,2,3,4] range(1,5) = [1,2,3,4] range(1,10,2) = [1,3,5,7,9] The list [0,1,2,3,4] Upper limit (exclusive) Lower limit (inclusive) step (distance between elements)

Making decisions How do we compute absolute value? abs(5) = 5

Absolute value solution If the value is negative, negate it. Otherwise, use the original value. val = -10 if val < 0: result = - val print result val = -10 if val < 0: result = - val else: result = val print result val = -10 if val < 0: print - val else: print val

The if body can be any statements # height is in km if height > 100: print “space” else: if height > 50: print “mesosphere” if height > 20: print “stratosphere” print “troposphere” # height is in km if height > 100: print “space” elif height > 50: print “mesosphere” elif height > 20: print “stratosphere” else: print “troposphere” # height is in km if height > 50: if height > 100: print “space” else: print “mesosphere” if height > 20: print “stratosphere” print “troposphere” then clause Execution gets here only if “height > 100” is false Execution gets here only if “height > 100” is false AND “height > 50” is true t else clause Question: why don’t we ever accidentally print “mesosphere” when height is 220? “height > 50” is true in that case. t e e troposphere stratosphere mesosphere space km above earth 10 20 30 40 50 60 70 80 90 100

The then clause or the else clause is executed if is_prime(x): y = x / 0 else y = x*x I hate this example. Come up with a real-world example where one branch always fails but isn’t always executed.