# Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1 For loop is a counter controlled loop. For loop is a pretest loop. Used when number.

## Presentation on theme: "Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1 For loop is a counter controlled loop. For loop is a pretest loop. Used when number."— Presentation transcript:

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1 For loop is a counter controlled loop. For loop is a pretest loop. Used when number of iterations is know before we start the loop. For Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 2 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; }

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 3 Example 1: What output is produced? int count ; for ( count = 4 ; count > 0 ; count-- ) { cout << count << endl; } cout << " Done " << endl; For Loop - Examples

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 4 Example 2: for (j = 1; j <= 3; j++) cout << "loop index is " << j << endl; Equivalent while loop j = 1; while (j <= 3) { cout << "loop index is "<< j << endl; j++; } For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 5 Example 3: What is output? for (count = 10; count != 0; count--) cout << count << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 6 For Loop – Examples … Example 4: What is output? cout << "\n\tNumber\tSquare\tCube " << endl; cout << "\t------\t------\t---- " << endl; for (int num = 1; num < 11; num++) cout << ' \t' << num << '\t' << num*num << '\t' << num*num*num << endl;

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 7 Example 5: Rewrite example 4 using a while loop. cout << "\n\tNumber\tSquare\tCube " << endl; cout << "\t------\t------\t---- " << endl; for (int num = 1; num < 11; num++) cout << '\t' << num << '\t' << num*num << '\t' << num*num*num << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 8 For Loop – Examples … Example 6: What is output? char letter; for(letter = 'A'; letter <= 'Z'; letter++) cout << letter;

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 9 For Loop – Examples … Example 7: What is output? for (j = 10; j >= 30; j += 5) cout << j << " ";

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 10 Example 8: Convert the following while statement to an equivalent for statement j = 1; sum = 0; while (j < =15) { sum += j; j += 2; } cout << sum << endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 11 Example 9: Trace for statement and show its output. for (j = 1; j < 10; j++) cout << setw(j) << '* ' <<endl; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 12 Example 10: In the String class, the length() function returns the number of characters in a String object. a) The following loop is designed to take a string and pad it on the right with *’s so that the length is expanded to 15. For example, Nancy becomes Nancy**********. Find the missing statement and loop test: cin >> str; ______________________ for (i=1; __________ ; i++) str += "*"; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 13 Example 10: b) Modify the loop in part a) to take a string and expand it on the left with *’s to a length of 15. cin >> str; For Loop – Examples …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 14 a) An “out of sync” header can result in the loop being skipped or in an infinite loop. // loop will be skipped for (j=1; j >=10; j++) cout << j << " "; // infinite loop for (j = 5; j > 0; j++) cout << j << " "; Cautions for For Loop

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 15 b) Don’t alter control variable within the loop body for (j = 1; j <= 10; j++) { cout << j << " "; j = j + 2;//DON’T DO THIS } Cautions for For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 16 c) Don’t put a semicolon after the for header, as any statement after the semicolon will be outside the loop. int count; for (count = 0; count < 10; count++) ; { cout << "*" ; } Cautions for For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 17 - The initialization statement of a for loop may contain two or more statements separated by commas. - The update part of a loop may contain multiple expressions that allow the loop to modify more than one control object. Example 1: Output? for (j = 1, k = 7; j <= k; j++, k--) cout << j+ 2 * k << " "; cout << endl; Generalized For Loop

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 18 Example 2: Give the output for each general for loop statement. a) for (sum=0, i=0, k=0 ; i<k; i++, k-- ) sum += 2 * i + k; Generalized For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 19 Example 2: Give the output for each general for loop statement. b) for (i=0, j=1; i*j<100; i++, j*=10) cout << i*j << endl; Generalized For Loop …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 20 Nested for loop is a loop within a loop Consists of an outer loop and an inner loop Example 1: What will be printed by the following code segments? for (n = 2; n <= 4; n++) { for (j = 6; j <= 7; j++) cout << n << ' ' << j << endl; cout << " j is now " << j <<endl; } cout << " n is now " << n << endl; Nested Loops

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 21 Example 2: What will be printed by the following program segments? cin >> num_rows; for (r = 1; r <= num_rows; r++) { // indent by printing num_rows -r spaces for (i = 1; i <= num_rows -r; i++) cout << ' ' ; // print r asterisks for (i = 1; i <= r; i++) cout << ' * ' ; // drop cursor to a new line cout << endl; } // end outer for Nested Loops …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 22 Want to set up the nested loops to produce the number triangle below: 1 // line number = 1 1 2 // line number = 2 1 2 3 // line number = 3 1 2 3 4 1 2 3 4 5 //.... 1 2 3 4 5 6 // line number = 6 1 2 3 4 5 6 7 //.... 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 // line number = 9 Designing Nested Loops

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 23 Form:break Action: At any place in a loop body, a break statement immediately transfers program control to the first statement following the loop. When a break statement is used as part of an inner nested loop, program control exits the inner loop but not the outer loop. We have seen the use of the break statement in the switch statement The break Statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 24 Example: The break statement is used in an exit test within an "infinite loop". The loop terminates on a response of 'Q'. while (true)// infinite while loop {... cin >> response;// request user input if (response == 'Q')// test for the QUIT response break; // break from the loop.... // continue the loop body } The break Statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 25 Example: Using a break statement with nested loops to produce a multiplication table for(int x = 1; x x) break; else cout << setw(4) << x*y; cout << endl; } The break Statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 26 Form:continue Action: The continue statement terminates execution of all remaining statements in the current iteration and passes program control to the next iteration. When continue is used in a while or do..while statement, the next code to execute is the loop test. The programmer is responsible to update control objects, as necessary, before executing continue. The continue statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 27 Form:continue Action …: In a for loop, the next code to execute is the update expression. The continue statement

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 28 Example: i = 1; while (i < 10) {... cin >> n;// input an integer value if (n < 0) // test for negative n { i++; // prepare for next iteration continue; // go to next iteration }...// otherwise continue loop body } The continue statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 29 The equivalent for statement also uses continue. for(i=1; i < 10; i++) {... if (n < 0) // test for negative n continue; // increment i and perform loop test... // otherwise continue loop body } The continue statement …

Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 30 What is the output from this program? int main() { for (int i = 0;i < 8;i++) { if (i%2 == 0) cout << i + 1 << endl; else if (i%3 == 0) continue; else if (i%5 == 0) break; else cout << " Not multiple of 2, 3 or 5.\n "; } cout << " End of program.\n "; } The continue/break statement

Download ppt "Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1 For loop is a counter controlled loop. For loop is a pretest loop. Used when number."

Similar presentations