Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition and Loop Statements Chapter 5. 2 Why iterate? t Example: Algorithm for searching a letter w in a string that consists of n letters 1. Extract.

Similar presentations


Presentation on theme: "Repetition and Loop Statements Chapter 5. 2 Why iterate? t Example: Algorithm for searching a letter w in a string that consists of n letters 1. Extract."— Presentation transcript:

1 Repetition and Loop Statements Chapter 5

2 2 Why iterate? t Example: Algorithm for searching a letter w in a string that consists of n letters 1. Extract first letter w 0 1.1 if(w == w 0 ) return; // w was found 2. Extract first letter w 1 1.1 if(w == w 1 ) return; // w was found … n. Extract first letter w n 1.1 if(w == w n ) return; // w was found n+1.// w was not found  n steps are needed (n if statements)  All these steps do practically the same task: extract a letter and check for its occurrence  Number of used statements rises with increasing string length!  Program must be changed if the number n changes!

3 3 Why iterate? t Use the computer's speed to do the same task faster than if done by hand. t Avoid writing the same statements over and over again. t Programs can be made general enough to handle other types of data.

4 4 Repetitive control structures –Because many algorithms require many iterations over the same statements. To average 100 numbers, we would need 300 plus statements. Or we could use a statement that has the ability to repeat a collection of statements

5 5 5.1 Counting Loops and the while Statement –General form of the while statement: while ( loop-test ) { iterative-part } –When a while loop executes, the loop-test is evaluated. If true (non-zero), the iterative part is executed and the loop-test is reevaluated. This process continues until the loop test is false. –While is a pre test loop

6 6 Collections of statements are delimited with { and } t Example: Average of 100 numbers while there is another number, do the following { cout << "Enter number: "; cin >> number; sum = sum + number; } average = sum / 100;

7 7 Example: counting loop t Display pay for 7 employees: countEmp = 0; while(countEmp < 7) { cout << “Hours: “; cin >> hours; cout << “Rate: “; cin >> rate; pay = hours*rate; cout << “Weekly pay is: “ << pay << endl; countEmp = countEmp + 1; }

8 8 Steps for counting loops t 1. Initialization of the control variable t 2. Test of the while condition –false:  exit loop (go to Step 6) –true:  execute body (Step 3) t 3. Execution of iterative part t 4. Update of control variable (e.g. increment) t 5. Go to step 2 t 6. First statement after the while loop

9 9 Flowchart view of while loop countEmp < 7 Get data Compute pay Display pay Iterative Part FalseTrue Inc countEmp by 1

10 10 –Infinite loop: a loop that never terminates. Termination occurs if, for example, the while condition becomes false. –Infinite loops are usually not desirable. –Here is an example of an infinite loop: Infinite loops

11 11 int counter = 1; int j = 0; int n = 3; while(counter <= n) { j = j + 1; cout << counter << endl; } cout << "Do we ever get here?" << endl; There is no step that brings us to termination. Infinite loops

12 12 Infinite loops t Another example: while(true) { statement(s)-1; // while body } statement(s)-2; // these are never executed (unless a break is used in the body) t Other extreme: No loop at all while(false) { statement(s)-1; // these are never executed } statement(s)-2;  (compare: if(false) doSomething;) t Infinite loops are a big source of (logic) faults in real programs !!

13 13 5.2 Accumulating a Sum or Product in a Loop t Lets look at the idea of adding together a group of numbers t Many lines of the same statements t Sample code without a loop

14 14 Sum 100 values the hard way int sum = 0; cout << "Enter number: "; // <-Repeat these three cin >> number; // <- statements for each sum = sum + number; // <- number in the set cout << "Enter number: "; cin >> number; sum = sum + number; /*...291 statements deleted... */ cout << "Enter number: "; cin >> number; sum = sum + number; average = sum / 100;  more than 300 lines of code with a while loop: only 8 lines !!! int sum, iterations; sum = iterations = 0; while(iterations < 100) { cout << "Enter number: "; cin >> number; sum = sum + number; iterations = iterations + 1; } average = sum/100;

15 15 Compound Assignment Operators t Increment variable++; is equivalent to variable = variable + 1; t Decrement variable--; is equivalent to variable = variable – 1; t Short hand notation totalPay += pay; is equivalent to totalPay = totalPay + pay; t In general variable op= expression; is equivalent to variable = variable op expression;

16 16 ComputePay.cpp // FILE: ComputePay.cpp // COMPUTES THE PAYROLL FOR A COMPANY // Number of Employees is variable #include #include "money.h" int main () { int numberEmp; int countEmp; float hours; money rate; money pay; money totalPay;

17 17 ComputePay.cpp // Get number of employees from user. cout << "Enter number of employees: "; cin >> numberEmp; // Compute each employee's pay and add it to // the payroll. totalPay = 0.0; countEmp = 0;

18 18 ComputePay.cpp while (countEmp < numberEmp) { cout << "Hours: "; cin >> hours; cout << "Rate : $"; cin >> rate; pay = hours * rate; cout << "Pay is " << pay << endl << endl; totalPay += pay; countEmp++; }

19 19 ComputePay.cpp cout << "Total payroll is " << totalPay << endl; cout << "All employees processed." << endl; return 0; }

20 20 ComputePay.cpp Program Output Enter number of Employees: 3 Hours: 50 Rate: $5.25 Pay is: $262.5 Hours: 6 Rate: $5 Pay is: $30 Hours: 1.5 Rate: $7 Pay is: $105 Total payroll is $397.5 All employees processed.

21 21 Break Statement t The break statement can be used in order to terminate the loop under program control and without relying on the test condition t Example: // read numbers until 0 is typed in while(true) { cin >> number; cout << number << endl; if(number == 0) break; }

22 22 Extraction operator and While loop t The extraction operator may be used in the test condition of a while loop t Example while(cin >> number) cout << number << endl; t Semantics: If there in any input, the “cin>>” returns a non-zero value (true), else the returned value is 0 (false) –Terminating the loop is OS-dependent –E.g. usage of the escape sequence Ctrl+d in Unix

23 23 5.3 The for Statement –The for loop is similar to the other C++ looping construct the while loop. –The for loop forces us to write, as part of the for loop, an initializing statement, the loop-test, and a statement that is automatically repeated for each iteration. –Pre test loop.

24 24 Example for loop –This is a for-loop version of a counter-controlled loop : –Scope of the loop control variable: for(int counter = 1; counter <= 5; counter = counter+1) { cout << counter << " "; } Output: ________________________________?

25 25 General form of a for loop for( initial statement ; loop-test ; repeated statement) { iterative-part } Steps: 1.Execute initial statement 2.Evaluate loop test: 2.1 false: Go to Step 6 2.2 true: Go to Step 3 3.Execute iterative-part 4.Execute repeated statement 5.Go to step 2 6.First Statement after the loop

26 26 Flowchart for a for loop Initial statement False countEmp < 7 Get Data Compute Pay Display Pay Increase countEmp by 1 True

27 27 Other In/De-crementing Operators –Increment: ++x; x++; –Decrement: --x; –In expressions: a) Post-increment (-decrement) n = m++; n = m--; b) Pre-increment (decrement) n = ++m; n = --m; –Avoid them in complex expressions, since compilers optimize code using commutativity: x = 5;i = 2; y = i *x + ++i ;(y may be 13 or 18)

28 28 More Examples t Displaying a String Letter by Letter string name; cout << “Enter your name: “ ; cin >> name; for(int pos = 0; pos < name.length(); pos++) cout << name.at(pos) << endl; t Displaying a table of Celsius/Fahrenheit pairs cout << Celsius” << “ Fahrenheit” << endl; for(int celsius = 10; celsius >= -5; celsius -=5) cout << setw(5) << celsius << setw(15) <<,,1.8*celsius + 32.0 << endl; Manipulator: setw(x) - set width to x - used to format output as a table.

29 29 5.4 Conditional Loops t In many programming situations, you will not be able to determine the exact number of loop repetitions t Conditional Loop –Initialize the loop control variable –While a condition involving the loop control variable is true – Continue processing – Update the loop control variable

30 30 Conditional Loops  Case Study:Monitoring Oil Supply –Problem: We want to monitor the amount of oil remaining in a storage tank at the end of each day. The initial supply of oil in the tank and the amount taken out each day are data items. Our program should display the amount left in the tank at the end of each day and it should also display a warning when the amount left is less than or equal to 10 percent of the tank’s capacity. At this point, no more oil can be removed until the tank is refilled.

31 31 Conditional Loops –Analysis: Clearly, the problem inputs are the initial oil supply and the amount taken out each day. The outputs are the oil remaining at the end of each day and a warning message when the oil left in the tank is less than or equal to 10 percent of its capacity. Constants to be used are the capacity of the tank and the 10% warning threshold. –Testing: To test the program, try running it with a few samples of input data. One sample should bring the oil level remaining to exactly 10 percent of the capacity. For example, if the capacity is 10,000 gallons, enter a final daily usage amount that brings the oil supply to 1,000 gallons and see what happens.

32 32 OilSupply.cpp // File: oilSupply.cpp // Displays daily usage and amount left in oil tank. #include using namespace std; const float CAPACITY = 10000;// tank capacity const float MINPCT = 10.0; // minimum percent float monitorOil(float, float);

33 33 OilSupply.cpp int main() { float supply; // input - initial oil supply float oilLeft; // output - oil left in tank float minOil; // minimum oil supply // Get the initial oil supply. cout << "Enter initial oil supply: "; cin >> supply;

34 34 OilSupply.cpp // Compute the minimum oil supply. minOil = CAPACITY * (MINPCT / 100.0); // Compute and display the amount of oil // left each day. oilLeft = monitorOil(supply, minOil); // Display a warning message if supply is less // than minimum cout << endl << oilLeft << " gallons left in tank." << endl;

35 35 OilSupply.cpp if (oilLeft < minOil) cout << "Warning - amount of oil left is below minimum!" << endl; return 0; }

36 36 OilSupply.cpp float monitorOil(float supply, float minOil) { // Local data... float usage; // input from user - Each day's oil use float oilLeft;// Amount left each day oilLeft = supply; while (oilLeft > minOil) { cout << "Enter amount used today: "; cin >> usage; oilLeft -= usage;

37 37 OilSupply.cpp cout << "After removal of " << usage << " gallons," << endl; cout << "number of gallons left is " << oilLeft << endl << endl; } // end while return oilLeft; } t More general conditional loops while(boolean expression) Example: while(oilLeft > minOil && numDazys < 14)

38 38 5.5 Loop Design and Loop Patterns t Sentinel-Controlled Loops 1. Read the first data item. 2. while the sentinel value has not been read 3. Process the data item. 4. Read the next data item. t Flag Controlled Loops 1. Set the flag to false. 2. while the flag is false 3. Perform some action. 4. Reset the flag to true if the anticipated event occurred.

39 39 Sentinel Loops –A sentinel is a specific predetermined value used to terminate one type of event-controlled loop. –It is the same type as the other extracted data (but not possibly valid data). –For example, the int constant -1 may act as a sentinel when all valid int data > -1

40 40 SumScores.cpp // File: SumScores.cpp // Accumulates the sum of exam scores. #include using namespace std; void displayGrade(int); int main() {

41 41 SumScores.cpp const int SENTINEL = -1; int score; int sum; int count; int average; // Process all exam scores until sentinel is read count = 0; sum = 0; cout << "Enter scores one at a time as requested." << endl; cout << "When done, enter " << SENTINEL << " to stop." << endl;

42 42 SumScores.cpp cout << "Enter the first score: "; cin >> score; while (score != SENTINEL) { sum += score; count++; displayGrade(score); cout << endl<< "Enter the next score: "; cin >> score; }

43 43 SumScores.cpp cout << endl << endl; cout << "Number of scores processed is " << count << endl; cout << "Sum of exam scores is " << sum << endl; // Compute and display average score. if (count > 0) { average = sum / count; cout << "Average score is " << average; } return 0; }

44 44 SumScores.cpp // Insert function displayGrade here. void displayGrade(int score) { // Insert function displayGrade here. }

45 45 SumScores.cpp Program Output Enter the scores one at a time as requested. When done, enter -1 to stop. Enter the first score: 55 Enter the next score: 33 Enter the next score: 77 Enter the next score: -1 Sum of exam scores is 165 3 exam scores were processed. Average of the exam scores is 55.0

46 46 Flag-Controlled Loops char getNextDigit() { char nextChar; bool digitRead; digitRead = false; while (!digitRead) { cin >> nextChar; digitRead = (('0'<= nextChar) && (nextChar <= '9')); } return nextChar; }

47 47 5.6 The do-while Statement –The do while statement is similar to the while loop, but the do while loop has the test at the end. General form: do { iterative-part } while ( loop-test ) ; –Notice the iterative part executes BEFORE the loop-test)

48 48 When to use the do-while loop –The do while loop is a good choice for obtaining interactive input from menu selections. –Consider a function that won't stop executing until the user enters an N, O, or S: –Post test loop

49 49 Example do-while loop char menuOption() { // POST: Return an upper case 'N', 'O' or 'S' char option; do { cout << "Enter N)ew, O)pen, S)ave: "; cin >> option; option = toupper(option); // from ctype.h } while (option != 'N' && option != 'O' && option != 'S'); return option; }

50 50 Largest.cpp // FILE: Largest.cpp // FINDS THE LARGEST NUMBER IN A SEQUENCE OF // INTEGER VALUES #include // needed for cin and cout #include // needed for INT_MIN using namespace std; int main () {

51 51 Largest.cpp // Local data... int itemValue; // each data value int largestSoFar; // largest value so far int minValue; // the smallest integer // Initialize largestSoFar to the smallest // integer. minValue = INT_MIN; largestSoFar = minValue;

52 52 Largest.cpp // Save the largest number encountered so far. cout << "Finding the largest value in a sequence: " << endl; do { cout << "Enter an integer or " << minValue << " to stop: "; cin >> itemValue; if (itemValue > largestSoFar) largestSoFar = itemValue; } while (itemValue != minValue);

53 53 Largest.cpp cout << "The largest value entered was " << largestSoFar << endl; return 0; } Program Output Finding the largest value in a sequence: Enter an integer or -32768 to stop: -999 Enter an integer or -32768 to stop: 500 Enter an integer or -32768 to stop: 100 Enter an integer or -32768 to stop: -32768 The largest value entered was 500

54 54 5.7 Review of while, for, and do-while Loops t while - Most commonly used when repetition is not counter controlled; condition test precedes each loop repetition; loop body may not be executed at all t for - Counting loop - When number of repetitions is known ahead of time and can be controlled by a counter; also convenient for loops involving non counting loop control with simple initialization and updates; condition test precedes the execution.

55 55 Review of while, for, and do- while Loops t do-while - Convenient when at least one repetition of loop body must be ensured. Post test condition after execution of body.

56 56 5.8 Nested Loops t Possible to nest loops –Loops inside other loops –Start with outer jump to inner loop –Inner loop continues until complete –Back to outer loop and start again t NestLoop.cpp and Triangle.cpp examples

57 57 Nested Logic outer = 1; while(outer <= 3) { inner = 1; // Resets for each iteration of the outer loop while(inner <= outer) { cout << outer << " " << inner << endl; inner = inner + 1; } // End of the nested loop outer = outer + 1; // Increment the outer loop counter } // The end of the outer loop

58 58 NestedLoops.cpp // FILE: NestedLoops.cpp // ILLUSTRATES A PAIR OF NESTED FOR LOOPS #include int main () { // display heading cout << setw(12) << "i" << setw(6) << "j" << endl;

59 59 NestedLoops.cpp for (int i = 0; i < 4; i++) { cout << "Outer" << setw (7) << i << endl; for (int j = 0; j < i; j++) cout << " Inner" << setw (10) << j << endl; } return 0; }

60 60 NestedLoops.cpp Program Output ij Outer0 Outer1 Inner0 Outer2 Inner0 Inner1 Outer3 Inner0 Inner1 Inner2

61 61 Displaying the Multiplication Table t Example: 0 1 2 3 4 … -------------------------------- 00 0 0 0 0 10 1 23 4 20 2 46 8 30 3 69 12 t Sample code: cout << “ 01 23 4 5 6 7 8 9”<< endl; cout << “-------------------------------------------------------------”<<endl; for (int row = 0; row < 10; row++) cout << setw(3) << row << “ “; for (int col = 0; col < 10; col++) cout << setw(3) << row*col; cout << endl;

62 62 Further General Features of Loops t You can call functions within loops t You can use the break statement to enforce exiting any loop (even for loops) t Called functions may also contain loops (indirect nesting) for (int i = 0; i < 10; i++) // this loop calls the function f() 10 times f(); t You can use the break statement to enforce exiting any loop (even for loops) t Counting loops (like for loops) can be in general converted into a while loop Example above as while loop: int i = 0; while(i < 10) { f(); i++; }

63 63 Further General Features of Loops t In C++ for-loops are as powerful as while-loops, any while-loop can be converted to a for-loop (other languages like Pascal use only counting loops for for-loops) Example: while(true)  for(;;) doSomething; doSomething; t Do-while loops can be converted to while loops and vice versa. Example: cout << “Enter number ”;  do { cin >> number; cout << “Enter number ”; while(number > number; cout << “Enter number ”; } while(number < 10) cin >> number; }

64 64 While and switch statements counter = 10; while(counter > 0) { cout << “Write a letter “; cin > ch; switch(ch) { case ‘A’: case ‘a’: cout << “Input was A or a” << endl; break; … case ‘Z’: case ‘z’: cout << “Input was Z or z” << endl;; break; } counter--; }

65 65 Break and continue in loops t As we know: Break means to quit the loop immediately t The continue statement has an opposite character: Continue means go to the test condition immediately t Example: int i = 0; while(i < 5) { if(I == 3) continue; cout << “Now the number is “ << I << endl; } Output: Now the number is 0 Now the number is 1 Now the number is 2 Now the number is 4

66 66 Break and continue in loops t Break and continue statements are often used in conjunction with if statements in order to exit the loop (break) or just abort one iteration of the loop (continue). t Frequent use of break/continue makes programs less readable and therefore less maintainable. t A good programming style is not to use them at all, or to use them only in special situations t Example: What does this short program segment???????????? for(int i = 0; i < 4; i++) { for(int j = 0; j <4; j++) { if(i==j) break; cout << “Inner: “<< i << “ “<<j<<endl; } if(i == j) continue; cout << “Outer: “<< i << “ “<<j<<endl; // this statement will never be executed }

67 67 5.9 Debugging and Testing Programs  Modern Integrated Development Environments (IDEs) include features to help you debug a program while it is executing.  If you cannot use a debugger, insert extra diagnostic output statements to display intermediate results at critical points in your program.

68 68 5.10 Common Programming Errors –Coding style and use of braces. –Infinite loops will “hang you up !!” –Use lots of comments before and after a loop. –Test various conditions of loops. –Add white space between code segments using loops. –Initialize looping variables or use internal loop control variables (lcv) in the for loop. –Off-by-1-Errors for(int i = 0; i < N; i++)  0 1 2… N-1 for(int i = 0; i <= N; i++)  0 1 2… N-1, N


Download ppt "Repetition and Loop Statements Chapter 5. 2 Why iterate? t Example: Algorithm for searching a letter w in a string that consists of n letters 1. Extract."

Similar presentations


Ads by Google