1 / 19

Loop Design

Loop Design. What goes into coding a loop. Considerations for Loop Design. There are basically two kinds of loops: Those that form some accumulated sum or product. Those that do some repeated processing.

beamc
Télécharger la présentation

Loop Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loop Design What goes into coding a loop

  2. Considerations for Loop Design There are basically two kinds of loops: Those that form some accumulated sum or product. Those that do some repeated processing. We will concentrate in the first loop form: those that form some accumulated sum or product. Three things have to be designed, in this order: What is done inside the loop. What has to be done before the loop starts in order for it to work properly (the loop initialization) How we end the loop.

  3. A loop skeleton Initialize variables. while/for/do statement loop code end of loop The important points are: Design the loop. Align variables so they have the correct values when the loop starts Decide on a good termination condition.

  4. Example 1 Let us write a loop to add some numbers which are read in. The loop body is very simple: cin >> number; total += number; so the whole thing becomes: Initialize variables while/for/do statement cin >> number; total += number end of loop output total

  5. Adding numbers (continued 1) What variables have to be initialized? The only variables used in the loop are number and total. number is being read in, does not need initialization; however, total does. Both need declarations. Before we look at the declaration, let's look at the initial value of total. What should it be? What should the value be if no numbers have been read? Yes, 0. So, this is the declaration/initialization part: int number; // number to add int total = 0; // Contains the sum of all read in numbers. Note that these declarations do not necessarily have to be at the beginning!

  6. Adding numbers (continued 2) This leaves something like: int total = 0; // Contains the sum of all read in numbers. while/for/do statement int number; cin >> number; total += number; end of loop cout << “Sum is “ << total << endl; Now we have to decide how we are ending the loop.

  7. Adding numbers (continued 3) There are many ways to end the loop; we will discuss two: Knowing beforehand how many items to add. Placing a marker at the end Let us do (1) first.

  8. Adding numbers (continued 4a) Program to add a sequence of numbers, knowing beforehand how many there are. For added versatility, we will read in how many numbers there are in the program. This leaves: int num_data; cin >> num_data; int total = 0; repeat num_data times: int number; cin >> number; total += number; end of loop cout << “Sum is “ << total << endl;

  9. Adding numbers (continued 5a) There are two simple ways to code “repeat num_data times”: The textbook has: for (count = 1; count <= num_data; count++).... but, among many C and C++ programmers, an idiom has developed: for (count = 0; count < num_data; count++)... You are free to use either form, but you will prefer to use the second one when working with arrays, vectors or strings, later on in this course. For that reason, I prefer to always use the second form. The complete program is on the next slide.

  10. Adding numbers (variant (a) final version) // Program to add a given amount of numbers // Michael Rothstein // 01/30/2014 #include <iostream> using std::cin; using std::cout; using std::endl; int main() { int num_data; cin >> num_data; int total = 0; for(int count = 0;count < num_data;count++){ int number; cin >> number; total += number; } cout << "Sum is " << total << endl; }

  11. Adding numbers, variant a(discussion) This example shows how to code a loop when we know in advance the number of times the loop should execute, or when we know, in advance, a maximum number of times that the loop should execute. This program may work in some situations, namely, when the number of data is small; however, if we have to count many numbers, we may have a problem. It is very hard to count a large number of things accurately. For that reason, let us look at an alternative way to end the loop.

  12. Adding numbers 1 (continued 3b) So, we are adding a column of numbers. As we discussed before, counting is error prone, so we should try to think up something else). One idea could be to end the numbers with a signal, a “fake” number. (This number is called a sentinel ). It should be a number that cannot occur in the data; for example, if all the numbers are positive, -1 could be a good sentinel value. We will again read in the numbers to add, one by one. Remember, our program skeleton:

  13. Adding numbers (continued 4b) int total = 0; // Contains the sum of all read in numbers. while/for/do statement int number; cin >> number; total += number; end of loop cout << “Sum is “ << total << endl; But, we have to check whether number is -1. That leaves:

  14. Adding numbers (continued 5b) int total = 0; // Contains the sum of all read in numbers. while ( number != -1){ int number; cin >> number; total += number; } cout << “Sum is “ << total << endl; Except that we are reading the number after checking for it! Two solutions: Read before and switch things around. No switch, just check after reading. Will explore both solutions in the next slides.

  15. Adding numbers (continued 6b1) In this alternative, we are going to switch things around: int total = 0; // Contains the sum of all read in numbers. int number; cin >> number; while ( number != -1){ total += number; cin >> number; } cout << “Sum is “ << total << endl; The final version is in the next slide.

  16. Adding numbers, option b1,final version // Program to add numbers until a -1 is input, version 1 // Michael Rothstein // 01/30/2014 #include <iostream> using std::cin; using std::cout; using std::endl; int main(){ int total = 0; int number; cin >> number; while (number != -1){ total += number; cin >> number; } cout << "Sum is " << total << endl; }

  17. Adding numbers (continued 6b2) In this version, we will simply check after reading. However, since we will not have read a number in the while statement statement, we will have to change the while statement to a nonsense condition: int total = 0; // Contains the sum of all read in numbers. while ( 1){ // This is actually what is usually done. Another idiom! int number; cin >> number; if ( number == -1 ) break; total += number; } cout << “Sum is “ << total << endl; The final version is on the next slide.

  18. Adding numbers, option b2,final version // Program to add numbers until a -1 is input, version 2 // Michael Rothstein // 01/30/2014 #include <iostream> using std::cin; using std::cout; using std::endl; int main() { int total = 0; // Contains the sum of all read in numbers. while (1){ int number; cin >> number; if( number == -1 ) break; total += number; } cout << "Sum is " << total << endl; }

  19. Adding numbers, final discussion Options b1 and b2 had an unpleasant situation: we basically had half of the loop before we knew whether to finish. This situation is known as loop and a half and the solutions presented are the best ones known at present. Some people prefer option b1 because they do not like to use break, whereas other people prefer b2 because they don't like multiple input statements. As you can see, programming is full of choices. There is not a set way to do many things. Also, C++ has many idioms; you are free to use them or adopt your own, but be consistent.

More Related