Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 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 Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { cout << num << “Potato” << endl; }
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Want to set up the nested loops to produce the number triangle below: 1 // line number = // line number = // line number = // // line number = // // line number = 9 Designing Nested Loops
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 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 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 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 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 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 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 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 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