Unformatted text preview:

University of Washington Computer Programming I Lecture 9 Iteration 2000 UW CSE H1 1 Overview Concepts this lecture Iteration repetitive execution Loops and nested loops while statements for statements H1 2 Chapter 5 Read Sections 5 1 5 6 5 10 5 1 Introduction 5 2 5 3 While statement 5 4 For statement 5 5 5 6 Loop design 5 7 Nested Loops 5 11 Common errors H1 3 An Old Friend Fahrenheit to Celsius include stdio h int main void double fahrenheit celsius printf Enter a Fahrenheit temperature scanf lf fahrenheit celsius fahrenheit 32 0 5 0 9 0 printf That equals f degrees Celsius celsius return 0 H1 4 What s Wrong with Fahrenheit Celsius Program User has to rerun the program for every new temperature Wouldn t it be nice if the program could process repeated requests Program ends immediately if user types a bad input Wouldn t it be nice the program politely asked the user again and again etc if necessary H1 5 One More Type of Control Flow Sometimes we want to repeat a block of code This is called a loop H1 6 Loops A loop is a repeated iterated sequence of statements Like conditionals loops iteration give us a huge increase in the power of our programs Alert loops are harder to master than if statements Even experienced programmers often make subtle errors when writing loops H1 7 Motivating Loops Problem add 4 numbers entered at the keyboard int sum int x1 x2 x3 x4 printf Enter 4 numbers scanf d d d d x1 x2 x3 x4 sum x1 x2 x3 x4 H1 8 Motivating Loops Problem add 4 numbers entered at the keyboard int sum int x1 x2 x3 x4 printf Enter 4 numbers scanf d d d d x1 x2 x3 x4 sum x1 x2 x3 x4 This works perfectly But what if we had 14 numbers or 40 or 4000 H1 9 Finding Repeated Code The key to using loops to solve a problem is to discover steps that can be repeated Our first algorithm for adding four numbers had no repeated statements at all But it does have some repetition buried in it Let s rework the algorithm to make the repetition more explicit H1 10 Add 4 Numbers Repetitively int sum x sum 0 printf Enter 4 numbers scanf d x sum sum x scanf d x sum sum x scanf d x sum sum x scanf d x sum sum x H1 11 Loop to Add 4 Numbers int sum x sum 0 printf Enter 4 numbers int sum x int count scanf d x sum sum x sum 0 printf Enter 4 numbers scanf d x sum sum x scanf d x sum sum x scanf d x sum sum x count 1 while count 4 scanf d x sum sum x count count 1 H1 12 More General Solution int sum x count int number inputs Number of inputs sum 0 printf How many numbers scanf d number inputs printf Enter d numbers number inputs count 1 while count number inputs scanf d x sum sum x count count 1 H1 13 while Statement Syntax Loop condition while condition statement1 Loop body Any statement statement2 or a compound statement H1 14 Compute 7 What is 1 2 3 4 5 6 7 seven factorial x 1 2 3 4 5 6 7 printf d x H1 15 Compute 7 What is 1 2 3 4 5 6 7 seven factorial x 1 2 3 4 5 6 7 printf d x Bite size pieces More Regular As a loop x 1 x 1 x 1 x x 2 x x i i i 1 i 2 x x 3 x x i i i 1 while i 7 x x 4 x x i i i 1 x x i x x 5 x x i i i 1 i i 1 x x 6 x x i i i 1 x x 7 x x i i i 1 i 2 H1 16 while Loop Control Flow x 1 i 2 i 7 no yes x x i i i 1 H1 17 Tracing the Loop What is 1 2 3 7 line i x x 1 A A 1 i 2 B while i 7 C x x i D i i 1 E F printf d x G H1 19 Tracing the Loop What is 1 2 3 7 line i x x 1 A i 2 B A B 1 1 while i 7 C x x i D i i 1 E 2 F printf d x G H1 20 Tracing the Loop What is 1 2 3 7 line i x i 7 x 1 A i 2 B A B C 1 1 1 T while i 7 C x x i D i i 1 E 2 2 F printf d x G H1 21 Tracing the Loop What is 1 2 3 7 line i x i 7 x 1 A i 2 B C 1 1 1 2 2 T while i 7 A B C D E x x i D i i 1 E 2 2 2 3 F printf d x G H1 22 Tracing the Loop What is 1 2 3 7 line i x i 7 x 1 A i 2 B C x x i D 1 1 1 2 2 2 T while i 7 A B C D E C i i 1 E 2 2 2 3 3 T F printf d x G H1 23 Tracing the Loop What is 1 2 3 7 line i x 1 A i 2 B while i 7 C x x i D i i 1 E A 1 B 2 1 C 2 1 D 2 2 E 3 2 C 3 2 C 7 720 D 7 5040 E 8 5040 F printf d x G x i 7 T T T H1 24 Tracing the Loop What is 1 2 3 7 line i x 1 A i 2 B while i 7 C x x i D i i 1 E A 1 B 2 1 C 2 1 D 2 2 E 3 2 C 3 2 C 7 720 D 7 5040 E 8 5040 C 8 5040 F printf d x G x i 7 T T T F H1 25 Tracing the Loop What is 1 2 3 7 line i x 1 A i 2 B while i 7 C x x i D i i 1 E A 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T C 7 720 T D 7 5040 E 8 5040 C 8 5040 F G Print 5040 F printf d x G x i 7 H1 26 Double Your Money Suppose your 1 000 is earning interest at 5 per year How many years until you double your money H1 27 Double Your Money Suppose your 1 000 is earning interest at 5 per year How many years until …


View Full Document

UW CSE 142 - Study Notes

Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?