Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2004-2005 Curt Hill A Quick Introduction to Looping Breadth not Depth.

Similar presentations


Presentation on theme: "Copyright 2004-2005 Curt Hill A Quick Introduction to Looping Breadth not Depth."— Presentation transcript:

1 Copyright 2004-2005 Curt Hill A Quick Introduction to Looping Breadth not Depth

2 Copyright 2004-2005 Curt Hill Flow of Control in C/C++ Recall the flow statements in C/C++: –Decisions If Switch Case –Loops for while do break Here we briefly consider the loops

3 Copyright 2004-2005 Curt Hill Flowchart Representation Sequential DecisionIteration

4 Copyright 2004-2005 Curt Hill Starting Example Suppose we want to average three numbers In a console program we could use the following code: double a,b,c; cin >> a >> b >> c; double avg = (a+b+c)/3; This works for three, but is it the best way for fifty?

5 Copyright 2004-2005 Curt Hill The Better Way Is a loop –Do the same thing over and over C++ has three loops: –while –for –do while One way to solve this problem is with a while –There are others as well

6 Copyright 2004-2005 Curt Hill The While Equivalent Instead we use the following code: int count = 0; double sum = 0, temp; while(count > temp; sum += temp; count++; } double avg = sum/50; This works for fifty values and is a lot less code

7 Copyright 2004-2005 Curt Hill While Loops A general loop Not always the best choice Execute a statement while a test is true Pieces: –A reserved word – while –A parenthesized condition –The body of the loop – one statement

8 Copyright 2004-2005 Curt Hill A General Thought Loops always have several pieces which we see in the above example An initialization –Setting count and sum to zero A test –Count less than 50 The body of the loop –Reading the value and summing it An advancement –Incrementing count Different loops handle these in different ways

9 Copyright 2004-2005 Curt Hill Example Revisited The above is not the best example of a while A for would do it in a nicer way: double sum = 0,temp, count; for(count=0;count > temp; sum += temp; } double avg = sum/50;

10 Copyright 2004-2005 Curt Hill For Considered A counting loop Several pieces: Reserved word – for Parenthesized controls –An initialization –A test –An advancement –These are separated by semicolons One statement –May also be a compound statement

11 Copyright 2004-2005 Curt Hill For Action The initialization is executed only once and is done first Next comes the test The statement is then executed if the test is true The advancement is then done Then go back to the test

12 Copyright 2004-2005 Curt Hill More for thoughts In the C family of languages the for is the most general of loops We can replace any while with a for We usually use a while when the advancement is not a counting operation or not at the end of the body Thus we use more for statements than any other loop

13 Copyright 2004-2005 Curt Hill While Revisited Suppose a cash register machine program We take in the purchase price and the amount the customer has given us Compute the change Make the change in terms of numbers of bills and coins the teller has to give Try first part of this with a while

14 Copyright 2004-2005 Curt Hill Cash Register Program double charge,tender; cin >> charge >> tender; double change = tender-charge; int twenties = 0; while(change >= 20.00){ change -= 20; twenties++; } if(twenties>0){ cout << “Give them” << twenties << “ twenties.”; }

15 Copyright 2004-2005 Curt Hill Example Revisited The loop would be duplicated for tens, fives, etc. Both the while and for are leading decision loops –They may execute the body of the loop zero times, if the condition is false –This is most common form of loop –This is desirable here, we may not give them one of some bill denomination Leading decision loops must initialize before the first test

16 Copyright 2004-2005 Curt Hill Another Situation Sometimes we end up in duplication in initialization and body of loop Consider the case of reading in the age of a hospital patient, that we attempt to verify int age; cin >> age; while(age 120){ cout > age; }

17 Copyright 2004-2005 Curt Hill Another Loop This type of situation may be best handled with a trailing decision loop This checks the condition at the end of the loop rather than the beginning Particularly handy for when the initialization is part of the loop processing C/C++’s trailing decision loop is called the do while

18 Copyright 2004-2005 Curt Hill do while loop Try this one more time: int age; do { cout >> “Enter age”; cin >> age; } while(age 120); The initialization is part of the loop itself

19 Copyright 2004-2005 Curt Hill Flowchart Representation Leading decision loopTrailing decision loop

20 Copyright 2004-2005 Curt Hill Determinism The first for and first while were deterministic –Before we executed the test the first time we could compute exactly how many times through the loop would be executed –Counting loops usually deterministic The last while and do while were non- deterministic –There is no way to predict the number of times through the loop –Often use whiles or do whiles for non- deterministic loops

21 Copyright 2004-2005 Curt Hill Conclusion This presentation has focused on comparisons not details or syntax Next we consider the individual loops and their details –for –while –do while


Download ppt "Copyright 2004-2005 Curt Hill A Quick Introduction to Looping Breadth not Depth."

Similar presentations


Ads by Google