1 / 17

Overview

Overview. Go over parts of quiz? Another iteration structure for loop. for loop. for (expression1; expression2; expression3) statement; Step 1: Evaluate expression1 Step 2: Evaluate expression2. If true, execute statement If false, continue execution at next statement

zita
Download Presentation

Overview

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. Overview • Go over parts of quiz? • Another iteration structure for loop

  2. for loop for (expression1; expression2; expression3) statement; Step 1: Evaluate expression1 Step 2: Evaluate expression2. If true, execute statement If false, continue execution at next statement following the for loop. Step3: Evaluate expression3 Step 4: Repeat Step 2 and 3 Example: Display the numbers from 1 to 10 for (int i = 1; i <= 10; i++) cout << i << endl;

  3. for loop with compound statement for (expression1; expression2; expression3) { statement1; statement2; } Example: Input 10 numbers and calculate the total int i, number, total = 0; for (i = 1; i <= 10; i++) { cout << “\nEnter next number: “; cin >> number; total = total + number; } cout << “The total of the numbers entered is “ << total << endl;

  4. Exercises • Use a for loop structure to solve the following • problems: • Display the numbers from 7 to 77 in steps of 7 • Display the numbers from 20 to 2 in steps of –2 • Display the following numbers 2,5,8,11,14,17,20 • Display the following numbers 99,88,77,66,55, • 44,33,22,11,0

  5. control variable for the for loop i is the control variable for the for loop in this example Initialize control variable Final value of control variable Update control variable for (i = 1; i <= 10; i++) cout << i << endl;

  6. Declaration of loop control variable inside for structure for (int i = 1; i <= 10; i++) cout << i << endl; instead of int i; for (i = 1; i <= 10; i++) cout << i << endl;

  7. for loops for (expression1; expression2; expression3) statement; is equivalent to: expression1; //Initialize loop control variable while (expression2) { //Test loop control variable statement; expression3; // Update loop control variable }

  8. Example: for loop vs while loop Display the numbers from 1 to 10 for (int i = 1; i <= 10; i++) cout << i << endl; OR int i = 1; while (i <= 10) { cout << i << endl; i++; }

  9. Example: change while loop to a for loop int sum = 0; int count = -5; while (count <= 15) { sum = sum + count; count ++; } int sum = 0; for (int count = -5; count <= 15; count++) sum = sum + count;

  10. When to use for loop Use the for loop for a counter controlled repetition Use the while loop for repetition when there is no counter. (i.e reading in input). for (int i = 1; i <= 10; i++) cout << i << endl; bool notDone = true; while (notDone) { cin << input; if (-1 == input) notDone = false; }

  11. Omitting expression1 for (; expression2; expression3) statement; Can omit expression 1 if loop control variable is initialized some place else. Note ; place holder Example: Display the numbers from 1 to 10 int i = 1; for (; i <= 10; i++) cout << i << endl;

  12. Omitting expression2 for (expression1; ; expression3) statement; When expression2 is omitted, C++ assumes the condition is always true, thereby creating an infinite loop Example: Display the numbers from 1 to infinity and beyond for (int i = 1; ; i++) cout << i << endl;

  13. Omitting expression3 for (expression1; expression2;) statement; When expression3 is omitted, this loop also becomes an infinite loop unless the loop control variable is updated inside the loop Example: Display the numbers from 1 to infinity and beyond for (int i = 1; i <= 10;) cout << i << endl;

  14. comma separated lists of expressions Expression1 and Expression3 may be a comma separated list of expressions. For example: int inputData; for (int i = 0, sum = 0; (i < 10) && (sum < 1000); i++) { cin >> inputData; sum = sum + inputData; } Note: Put only expressions involving the control variables in the initialization and increment expressions of the for structure.

  15. common programming error Changing the loop control variable inside the body of the loop is allowed but dangerous. for (int i = 0; i < 10; i++) { cin >> i; }

  16. common programming error Problem: Vary the control variable over the following sequence of values 2, 5, 8, 11, 14, 17, 20 Solution1: for (int i = 2; i <= 20; i +=3) Solution2: for (int i = 2; i < 22; i +=3) Solution2 more confusing. More likely to lead to boundary condition problems.

  17. Exercises • Write a program that inputs 10 grades, determines the highest grade and displays the result. Use a for loop. • Write the same program as in problem number 1 using a while loop.

More Related