# Lecture 4A

## Lecture 4A

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Lecture 4A Repetition Richard Gesick

2. Overview Iteration 3 kinds of loops for while dowhile Infinite Loops

3. Example: Computing Velocity over a Range of Time. • Sequential structures are not well suited for computing the same quantities repeatedly. • Flow charts and pseudocode are useful to describe repetition.

4. Looping In computing, we often need to perform the same operations on multiple items. Typically, these tasks follow this pattern: initialize values (set total to 0) process items one at a time (add price to total) report results (report total) The flow of control that programmers use to complete jobs with this pattern is called looping, or repetition.

5. Iteration One thing that computers do well is repeat commands Programmers use loops to accomplish this 3 kinds of loops in C++ for loop while loop do while loop

6. Criteria for loops Usually have some initial condition Starting a counter Beginning in a certain state Must have a test to continue Must make progress towards finishing

7. Loops in Everyday Life Bad children are told to write sentences on the board “I will not pour Clorox in the fish tank” Have to write this sentence either A certain number of times Until the teacher is happy As many as you can during break

8. The for Loop Ideal when you know the number of iterations to perform before the loop begins Examples: Find the sum of 5 numbers Find the maximum of 20 numbers Print the odd numbers from 1 to 10

9. The for loop Good when you know exactly how many times you need to execute something Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again } Initialization is where the counter is given a starting value The test determines whether or not to continue The increment can be any amount, including negative, and occurs after the loop statements execute

10. More for Loop Syntax for ( initialization; loop condition; loop update ) { // loop body } Notes: semicolons separate terms in the loop header no semicolon follows the loop header curly braces are required only if more than one statement is in the loop body

11. for Loop Flow of Control

12. for Loop Flow of Control The initialization statement is executed (once only). The loop condition is evaluated. If the condition is true, the loop body is executed. The loop update statement is executed, and the loop condition is reevaluated (#2). And so on, until the condition is false.

13. Satisfying the Teacher Example: 1000 sentences? No problem… intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<“I will not pour Clorox…”<<endl; } // Remember, counter++ is the same as // counter = counter + 1

14. “But I want them numbered!” No problem… intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; }

15. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 1

16. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter true 1

17. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 1 I will not pour Clorox in the Fish Tank counter 1

18. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 2

19. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter true 2

20. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 2 I will not pour Clorox in the Fish Tank counter 2

21. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 3

22. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter true 3

23. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 3 I will not pour Clorox in the Fish Tank counter 3

24. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 4

25. When will it end? We see that this will go on for a while It’s a little more interesting later around 1000

26. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter true 999

27. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 999 I will not pour Clorox in the Fish Tank counter 999

28. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 1000

29. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter true for last time 1000

30. Why this works(are we finished?) intcounter; for(counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } Output: 1000 I will not pour Clorox in the Fish Tank counter 1000

31. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } counter 1001

32. Why this works intcounter; for (counter = 1; counter <= 1000; counter++) { cout<<counter<<“ I will not pour…”<<endl; } // Jump down here and continue … … counter false 1001

33. Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank. . . . 999 I will not pour Clorox in the fish tank. 1000 I will not pour Clorox in the fish tank.

34. Another Example of Repetition int num; for ( num = 1 ; num <= 3; num++) { cout<<num << “ Potato”<<endl; }

35. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num ? OUTPUT

36. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 1 OUTPUT

37. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; 1 num true OUTPUT

38. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 1 OUTPUT 1Potato

39. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 2 OUTPUT 1Potato

40. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; 2 num true OUTPUT 1Potato

41. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 2 OUTPUT 1Potato 2Potato

42. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 3 OUTPUT 1Potato 2Potato

43. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; 3 num true OUTPUT 1Potato 2Potato

44. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 3 OUTPUT 1Potato 2Potato 3Potato

45. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; num 4 OUTPUT 1Potato 2Potato 3Potato

46. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; 4 num false OUTPUT 1Potato 2Potato 3Potato

47. int num; for ( num = 1 ; num <= 3; num++) cout<<num << “ Potato”<<endl; 4 num false When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.

48. The output was: 1Potato 2Potato 3Potato

49. int count ; for ( count = 4 ; count > 0 ; count-- ) { cout<<count<<endl; } cout<<“Done”<<endl; OUTPUT: 4 3 2 1 Done

50. The while Loop The while loop is designed for repeating a set of operations on data items when we don't know how many data items there will be. We will get some signal when we have reached the end of the items to process. The end of data items could be indicated by a special input value called a sentinel value or by reaching the end of a file Receiving the signal is an event; we call this event-controlled looping