CSE 142 Computer Programming I Overview Concepts this lecture Iteration repetitive execution Loops and nested loops while statements for statements Iteration 2000 UW CSE H1 1 H1 2 An Old Friend Fahrenheit to Celsius Chapter 5 include stdio h 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 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 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 One More Type of Control Flow Sometimes we want to repeat a block of code This is called a loop H1 6 H1 1 Loops Motivating Loops Problem add 4 numbers entered at the keyboard 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 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 7 H1 8 Finding Repeated Code Add 4 Numbers Repetitively The key to using loops to solve a problem is to discover steps that can be repeated int sum x sum 0 printf Enter 4 numbers Our first algorithm for adding four numbers had no repeated statements at all scanf d x sum sum x But it does have some repetition buried in it scanf d x sum sum x Let s rework the algorithm to make the repetition more explicit scanf d x sum sum x H1 9 H1 10 scanf d x sum sum x Loop to Add 4 Numbers 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 int sum x int count sum 0 printf Enter 4 numbers count 1 while count 4 scanf d x sum sum x count count 1 H1 11 while Statement Syntax Loop condition while condition statement1 Loop body Any statement statement2 or a compound statement H1 12 scanf d x sum sum x H1 2 More General Loop to Add Numbers Compute 7 int sum x count int number inputs What is 1 2 3 4 5 6 7 seven factorial x 1 2 3 4 5 6 7 printf d x 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 Compute 7 while Loop Control Flow 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 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 x 1 i 2 x 1 H1 15 Tracing the Loop x i 7 yes x x i i i 1 What is 1 2 3 7 line i x 1 i 2 while i 7 x x i i i 1 printf d x A 1 B 2 1 C 2 1 T D 2 2 E 3 2 C 3 2 T C 6 120 T D 6 720 E 7 720 C 7 720 T D 7 5040 E 8 5040 C 8 5040 F G Print 5040 A B C D E F G H1 14 i 7 no H1 16 Double Your Money Suppose your 1 000 is earning interest at 5 per year How many years until you double your money H1 17 my money 1000 0 n 0 while my money 2000 0 my money my money 1 05 n n 1 H1 18 printf My money will double in d years n H1 3 Printing a 2 D Figure Average Inputs printf Enter values to average end with 1 0 n sum 0 0 count 0 sentinel scanf lf next while next 1 0 sum sum next count count 1 scanf lf next if count 0 printf The average is f n H1 19 sum double count Nested Loop repeat 3 times print a row of 5 stars repeat 5 times print It seems as if a loop within a loop is needed H1 20 Nested Loop define ROWS 3 define COLS 5 row 1 while row ROWS print a row of 5 s row row 1 H1 21 row 1 while row ROWS print a row of 5 s col 1 while col COLS outer loop printf print 3 col col 1 rows printf n row row 1 inner loop print one row H1 22 Print a Multiplication Table Trace row 1 col How would you print the following diagram 2 3 1 2 345 6 1 2 345 6 output 4 1 2 345 6 row 1 while row ROWS print a row of 5 s col 1 while col COLS printf col col 1 H1 23 printf n row row 1 1 2 3 1 2 3 1 1 2 3 1 1 1 1 2 1 3 2 2 4 6 2 2 1 2 2 2 3 3 3 6 9 3 3 1 3 2 3 3 4 4 8 12 4 4 1 4 2 4 3 H1 24 H1 4 1 2 3 1 1 2 3 2 2 4 6 3 3 6 9 4 4 8 12 Nested Loops Print Row 2 col 1 while col 3 printf 4d 2 col col col 1 printf n row numberH1 25 Loop Trace row col 1 1 print 1 2 print 2 3 print 3 print n 2 1 print 2 2 print 4 3 print 6 print n Print 4 rows row 1 Print one row while row 4 col 1 while col 3 printf 4d row col col col 1 printf n row row 1 H1 26 Notes About Loop Conditions row col 3 1 print 3 2 print 6 3 print 9 print n 4 1 print 4 2 print 8 3 print 12 print n They offer all the same possibilities as conditions …
View Full Document
Unlocking...