1 / 13

An Introduction to LOOPS Objectives Acquiring a general knowledge of loops.

An Introduction to LOOPS Objectives Acquiring a general knowledge of loops. Understanding ONE way to create a loop in C++ -> the while statement. Learning the meaning of counters and how to implement them. How to read a set of numeric values and determine -> the sum of the values

tassos
Télécharger la présentation

An Introduction to LOOPS Objectives Acquiring a general knowledge of loops.

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. An Introduction to LOOPS Objectives Acquiring a general knowledge of loops. Understanding ONE way to create a loop in C++ -> the while statement. Learning the meaning of counters and how to implement them. How to read a set of numeric values and determine -> the sum of the values -> the average of the values -> the maximum and minimum of the values Ability to understand and write sentinel-controlled loops count-controlled loops general loops

  2. Why Do We Need Loops? Two Fundamental Properties of Computers 1. Fast 2. Accurate We use both of these characteristics in loops. We write applications which perform repetitive work because of the ability to repeat with precision and speed. EXAMPLE: Searching all student registration files to create a listing of all students in my class. As mentioned previously, one of the three basic building blocks (structures) of programming, .. REPETITION. AS WE WILL SOON SEE, THE REAL WORLD IS BUILT FROM THINGS WHICH REPEAT

  3. Your First Looping Statement Before we start remember when you tell the computer to repeat itself, you must also tell it how to determine when to stop! use the questions (logical expressions) learned in the previous chapter to indicate when to stop BE SURE YOU BELIEVE IT WILL STOP! WHILE statement gives us one of a number of waysto repeat statements. while ( logical expression ) statement; e.g. while ( deposit < 2 * original_amt) ... In order to be sure this loop terminates, either deposit, original_amt or both must change as we execute the loop. Note this in the next example!

  4. A Simple Example : How Much Money In the Bank? Input : Original amount deposited in the bank; Interest rate Process: Determine how many years to double money. Output: Number of years required to double your money. #include <iostream.h> void main () { float cur_amt, original_amt, int_rate; int years_to_double; cin >> original_amt >> int_rate; cur_amt = original_amt; // initialize while (cur_amt < (2*original_amt)) // test { cur_amt = cur_amt * (1+int_rate); } // body } We forgot to: Output anything or determine how many years it took. Note the 3 parts of the loop: initialization, test, body.

  5. Fixing the problem: COUNTING In order to fix the previous problem we need to Teach the computer to COUNT. 1. Before we enter the loop, we initialize the counter to 0. If we initialize in the body, we will continue to start over. Did you see Bill Murray inGroundHog Day? 2. Each time the loop is executed, increment the loop. #include <iostream.h> void main () { float cur_amt, original_amt, int_rate; int years_to_double; cin >> original_amt >> int_rate; years_to_double = 0; cur_amt = original_amt; // initialize while (cur_amt < (2*original_amt) ) // test { cur_amt= cur_amt * (1+int_rate); // body years_to_double = years_to_double + 1; }//end body cout << " At interest rate " << int_rate; cout << " it takes " << years_to_double << " to double" << endl; } Note your text shows a shortcut for incrementation years_to_double++;

  6. Reading input with loops Typically used to read different data sets without having to rerun the program. How do we stop? Consider a simple program which will read and print. 1. Sentinel loops - until a special value is found .... cin >> x; // read ahead while (x >= 0) // negative is sentinel { cout << x << endl; // output read ahead cin >> x; // read the next value } Read ahead is strange (but necessary) What if there's no obvious sentinel value to use?

  7. Reading input with loops (cont.) (more on Sentinel loops) If interactive, ask the user if there is more data? char response = 'Y'; int x; while (response == 'Y') { cin >> x; // read the value cout << x << endl; // write it out cout << " More data to enter(Y/N)?"; cin >> response; // see if there is more } More natural .. read then write. 2. Read until end_of_file comes later! One example p. 96 better examples later

  8. More looping strategies 3. Count controlled loop Used when you need to repeat a KNOWN number of times Use a counter as the test to terminate. { float cur_amt, original_amt, int_rate; int years_to_calc; int years_done=0; //initialize counter cin >> original_amt >> int_rate; cout << "Enter number of years to calculate"; cin >> years_to_calc; cur_amt = original_amt; while ( years_done < years_to_calc ) // test { cur_amt = cur_amt * (1+int_rate); years_done++;} // increment cout << " At interest rate " << int_rate; cout << " after " << years_to_double << " years the deposit is" << deposit << endl; } or the strategy can be used to read data. Write a simple program to ask the user to enter how many pieces of data they will enter, then read and output the values. Is this better than the sentinel methods?

  9. Summary We have seen a number of types of loops which will be used repeatedly in our applications. The hard part is determining which one to use! For loops executing a KNOWN number of times use > a count-controlled loop For loops executing an UNKNOWN number of times use > event controlled (deposit doubles) > sentinel controlled (until negative read) > end-of-file (when we learn it)

  10. Applications of loops We've seen counting. How about summing? 1. initialize the sum to zero (in addition to the counter if there is one) 2. add the next value to the sum (inside the loop) 3. output the result after the loop terminates float x, sum=0.0; // STEP 1 char response = 'Y'; while (response == 'Y') { cout << "Enter the next value"; cin >> x; // read the value sum = sum + x; // STEP 2 cout << "More data to enter(Y/N)? "; cin >> response; // see if there is more } // STEP 3 below cout << " The sum of the values is " << sum <<endl;

  11. Applications of loops (cont.) Trace the execution: X SUM Enter the next value 3 3 0 before STEP2 3 3 after STEP2 More data to enter(Y/N)?Y Enter the next value 5 5 3 before STEP2 5 8 after STEP2 More data to enter(Y/N)?N The sum of the values is 8

  12. Averaging Similar to adding, but add in counting. int num_values=0; float sum=0.0,x; .... char response = 'Y'; while (response == 'Y') { cout << "Enter the next value"; cin >> x; sum = sum + x; num_values++; cout << "More data to enter(Y/N)? "; cin >> response; } cout << " The sum of the values is " << sum <<endl; if (num_values > 0) cout << "The average is "<<sum/num_values<<endl; else cout <<"Average not defined for zero values!\n";

  13. Maximum and Minimum // using a count-controlled loop cout << "Enter the number of values to process"; cin >> num_values; maximum=-1.0; // assuming negative values // guess a number which is less // than ANY number you are using values_read = 0; while (values_read <num_values) { cout << "Enter the next value"; cin >> x; if (x > maximum) // compare this to maximum maximum = x; // replace maximum if this // value (x) is larger values_read++; } cout << " The maximum of the values is "; cout << maximum <<endl;

More Related