Control flow Ruth Anderson UW CSE 160 Spring 2015 1.

Slides:



Advertisements
Similar presentations
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Advertisements

Introduction to Computing Science and Programming I
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Geography 465 Assignments, Conditionals, and Loops.
Introduction to Python and programming Michael Ernst UW CSE 190p Summer 2012.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
If statements while loop for loop
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Introduction to Python and programming Ruth Anderson UW CSE 140 Winter
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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.
COMP Loop Statements Yi Hong May 21, 2015.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control flow Ruth Anderson UW CSE 160 Winter
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
REPETITION CONTROL STRUCTURE
Chapter 3: Decisions and Loops
Introduction to Python and programming
2-D Lists Taken from notes by Dr. Neil Moore
Topics Introduction to Repetition Structures
Ruth Anderson UW CSE 160 Spring 2018
Ruth Anderson UW CSE 160 Winter 2017
Introduction to Python and programming
Selection CIS 40 – Introduction to Programming in Python
Outline Altering flow of control Boolean expressions
Introduction to Python and programming
Loop Structures and Booleans Zelle - Chapter 8
Java Programming Loops
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
String and Lists Dr. José M. Reyes Álamo.
Control flow : if statements
Introduction to Python and programming
Logical Operators and While Loops
For loops Taken from notes by Dr. Neil Moore
Java Programming Loops
Topics Introduction to Repetition Structures
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Michael Ernst UW CSE 190p Summer 2012
EE 194/BIO 196: Modeling biological systems
2-D Lists Taken from notes by Dr. Neil Moore
Introduction to Python and programming
Programming Fundamental
Control flow: Loops UW CSE 160.
Control 9 / 25 / 19.
Presentation transcript:

Control flow Ruth Anderson UW CSE 160 Spring

Repeating yourself Making decisions 2

Temperature conversion chart Recall exercise from previous lecture fahr = 30 cent = (fahr - 32) / 9.0 * 5 print fahr, cent fahr = 40 cent = (fahr - 32) / 9.0 * 5 print fahr, cent fahr = 50 cent = (fahr - 32) / 9.0 * 5 print fahr, cent fahr = 60 cent = (fahr - 32) / 9.0 * 5 print fahr, cent fahr = 70 cent = (fahr - 32) / 9.0 * 5 print fahr, cent print "All done" Output: All done 3

Temperature conversion chart A better way to repeat yourself: for f in [30,40,50,60,70]: print f, (f-32)/9.0*5 print "All done" Loop body is indented A list Indentation is significant for loop Execute the body 5 times: once with f = 30 once with f = 40 … loop variable or iteration variable Output: All done Colon is required 4

i = 1 print i i = 4 print i i = 9 print i How a loop is executed: Transformation approach for i in [1,4,9]: print i State of the computer: Printed output: i: 1i: 4i: 9 Idea: convert a for loop into something we know how to execute 1.Evaluate the sequence expression 2.Write an assignment to the loop variable, for each sequence element 3.Write a copy of the loop after each assignment 4.Execute the resulting statements 5

for i in [1,4,9]: print i How a loop is executed: Direct approach Printed output: i: 1i: 4i: 9 Current location in list State of the computer: 1.Evaluate the sequence expression 2.While there are sequence elements left: a)Assign the loop variable to the next remaining sequence element b)Execute the loop body 6

The body can be multiple statements Execute whole body, then execute whole body again, etc. for i in [3,4,5]: print "Start body" print i print i*i Convention: often use i or j as loop variable if values are integers This is an exception to the rule that variable names should be descriptive Output: Start body 3 9 Start body 4 16 Start body 5 25 NOT: Start body loop body: 3 statements 7

Indentation is significant Every statement in the body must have exactly the same indentation That’s how Python knows where the body ends 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" for f in [30,40,50,60,70]: print f, (f-32)/9.0*5 print "All done" Error! 8

The body can be multiple statements How many statements does this loop contain? for i in [0,1]: print "Outer", i for j in [2,3]: print " Inner", j print " Sum", i+j print "Outer", i What is the output? Output: Outer 0 Inner 2 Sum 2 Inner 3 Sum 3 Outer 0 Outer 1 Inner 2 Sum 3 Inner 3 Sum 4 Outer 1 loop body: 3 statements “nested” loop body: 2 statements 9

Key idea: 1.Assign each sequence element to the loop variable 2.Duplicate the body Understand loops through the transformation approach 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 print "Outer", i for j in [2,3]: print " Inner", j i = 0 print "Outer", i j = 2 print " Inner", j j = 3 print " Inner", j i = 1 print "Outer", i for j in [2,3]: print " Inner", j 10

Fix this loop # 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) 11

Some Fixes for tens_digit in [0, 1, 2, 3, 4]: for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: print tens_digit * 10 + ones_digit + 1 for tens_digit in [0, 1, 2, 3, 4]: for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: print tens_digit * 10 + ones_digit for ones_digit in [1, 2, 3, 4, 5, 6, 7, 8, 9]: print ones_digit for tens_digit in [1, 2, 3, 4]: for ones_digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: print tens_digit * 10 + ones_digit print 50 12

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

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) 14

Some Loops # Sum of a list of values, what values? result = 0 for element in range(5): result = result + element print "The sum is: " + str(result) # Sum of a list of values, what values? result = 0 for element in range(5,1,-1): result = result + element print "The sum is:", result # Sum of a list of values, what values? result = 0 for element in range(0,8,2): result = result + element print "The sum is:", result # Sum of a list of values, what values? result = 0 size = 5 for element in range(size): result = result + element print "When size = " + str(size) + " result is " + str(result) 15

Some More Loops for size in [1, 2, 3, 4]: result = 0 for element in range(size): result = result + element print "size=" + str(size) + " result=" + str(result) print " We are done!" 16 What happens if we move result = 0 to be the first line of the program instead?

Decomposing a list computation To compute a value for a list: – Compute a partial result for all but the last element – Combine the partial result with the last element Example: sum of a list: [ 3, 1, 4, 1, 5, 9, 2, 6, 5 ] List b List a sum(List a) = sum(List b) + 5 sum(List b) = sum(List c) + 6 … sum(List y) = sum(List z) + 3 sum(empty list) = 0 List c List y List z 17

How to process a list: One element at a time A common pattern when processing a list: result = initial_value for element in list : result = updated result use result initial_value is a correct result for an empty list As each element is processed, result is a correct result for a prefix of the list When all elements have been processed, result is a correct result for the whole list # Sum of a list result = 0 for element in mylist: result = result + element print result 18

Examples of list processing Product of a list: result = 1 for element in mylist: result = result * element Maximum of a list: result = mylist[0] for element in mylist: result = max(result, element) Approximate the value 3 by 1 + 2/3 + 4/9 + 8/ /81 + … = (2/3) 0 + (2/3) 1 + (2/3) 2 + (2/3) 3 + … + (2/3) 10 result = 0 for element in range(11): result = result + (2.0/3.0)**element result = initial_value for element in list : result = updated result The first element of the list (counting from zero) 19

Making decisions How do we compute absolute value? abs(5) = 5 abs(0) = 0 abs(-22) = 22 20

Absolute value solution If the value is negative, negate it. Otherwise, use the original value. val = -10 # calculate absolute value of val if val < 0: result = - val else: result = val print result val = -10 if val < 0: print - val else: print val 21 In this example, result will always be assigned a value. Another approach that does the same thing without using result :

Absolute value solution As with loops, a sequence of statements could be used in place of a single statement: val = -10 # calculate absolute value of val if val < 0: result = - val print "val is negative!" print "I had to do extra work!" else: result = val print "val is positive" print result 22

Absolute value solution What happens here? val = 5 # calculate absolute value of val if val < 0: result = - val print "val is negative!" else: for i in range(val): print "val is positive!" result = val print result 23

Another if It is not required that anything happens… val = -10 if val < 0: print "negative value!" 24 What happens when val = 5?

Execution gets here only if “height > 100” is false AND “height > 50” is true The if body can be any statements # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" else: if height > 20: print "stratosphere" else: 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" else: if height > 20: print "stratosphere" else: print "troposphere" then clause else clause t e t e tropospherestratospheremesospherespace km above earth Execution gets here only if “height > 100” is false 25

Execution gets here only if “height <= 100” is true AND “height > 50” is true Version 1 # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" else: if height > 20: print "stratosphere" else: print "troposphere" then clause else clause t e t e tropospherestratospheremesospherespace km above earth Execution gets here only if “height <= 100” is true 26

Version tropospherestratospheremesospherespace km above earth 27 # height is in km if height > 100: print "space" else: if height > 50: print "mesosphere" else: if height > 20: print "stratosphere" else: print "troposphere"

Version 2 if height > 50: if height > 100: print "space" else: print "mesosphere" else: if height > 20: print "stratosphere" else: print "troposphere" tropospherestratospheremesospherespace km above earth 28

Version 3 if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" else: print "troposphere" tropospherestratospheremesospherespace km above earth 29 ONE of the print statements is guaranteed to execute: whichever condition it encounters first that is true

Order Matters # version 3 if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" else: print "troposphere" tropospherestratospheremesospherespace km above earth 30 # broken version 3 if height > 20: print "stratosphere" elif height > 50: print "mesosphere" elif height > 100: print "space" else: print "troposphere" Try height = 72 on both versions, what happens?

Version 3 # incomplete version 3 if height > 100: print "space" elif height > 50: print "mesosphere" elif height > 20: print "stratosphere" tropospherestratospheremesospherespace km above earth 31 In this case it is possible that nothing is printed at all, when?

What Happens here? # height is in km if height > 100: print "space" if height > 50: print "mesosphere" if height > 20: print "stratosphere" else: print "troposphere" tropospherestratospheremesospherespace km above earth 32 Try height = 72

The then clause or the else clause is executed speed = 54 limit = 55 if speed <= limit: print "Good job!" else: print "You owe $", speed/fine 33 What if we change speed to 64?