For Loop For loop is a counter controlled
- Slides: 30
For Loop • 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. SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 1
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 2
For Loop - Examples Example 1: What output is produced? int count ; for ( count = 4 ; count > 0 ; count-- ) { cout << count << endl; } cout << "Done " << endl; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 3
For Loop – Examples … 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++; } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 4
For Loop – Examples … Example 3: What is output? for (count = 10; count != 0; count--) cout << count << endl; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 5
For Loop – Examples … Example 4: What is output? cout << "nt. Numbert. Squaret. Cube " << endl; cout << "t------t---- " << endl; for (int num = 1; num < 11; num++) cout << 't' << num*num << endl; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 6
For Loop – Examples … Example 5: Rewrite example 4 using a while loop. cout << "nt. Numbert. Squaret. Cube " << endl; cout << "t------t---- " << endl; for (int num = 1; num < 11; num++) cout << 't' << num*num << endl; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 7
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 8
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 9
For Loop – Examples … 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 10
For Loop – Examples … Example 9: Trace for statement and show its output. for (j = 1; j < 10; j++) cout << setw(j) << '* ' <<endl; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 11
For Loop – Examples … 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 += "*"; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 12
For Loop – Examples … 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 13
Cautions for For Loop 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 << " "; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 14
Cautions for For Loop … 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 } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 15
Cautions for For Loop … 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 << "*" ; } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 16
Generalized For Loop - 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 17
Generalized For Loop … 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 18
Generalized For Loop … 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 19
Nested Loops • 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; Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 20
Nested Loops … 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 Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 21
Designing Nested Loops Want to set up the nested loops to produce the number triangle below: 1 1 2 3 4 5 1 2 3 4 5 6 7 8 9 Copyright © Nancy Acemian 2004 // line number = 1 // line number = 2 // line number = 3 //. . // line number = 6 //. . // line number = 9 For Loops-Break-Continue COMP 218 22
The break Statement 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 Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 23
The break Statement … 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 } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 24
The break Statement … Example: Using a break statement with nested loops to produce a multiplication table for(int x = 1; x <= 12; x++) { for (int y = 1; y <= 12; y++) if (y > x) break; else cout << setw(4) << x*y; cout << endl; } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 25
The continue statement 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. Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 26
The continue statement Form: continue Action …: • In a for loop, the next code to execute is the update expression. Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 27
The continue statement … 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 } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 28
The continue statement … 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 } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 29
The continue/break statement 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 "; } Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP 218 30
- Counter control loop
- Find the output of the following program
- Iterate counter python
- Decision making and looping in c
- Flag controlled loop c++
- Event-controlled loop
- Sentinel value
- Repetition control structure java
- Flag controlled loop c++
- Multi loop pid controller regolatore pid multi loop
- Manakah yang lebih baik open loop atau close loop system
- Kompor lpg termasuk open loop atau close loop
- Pernyataan do while
- Fifth gear loop the loop
- Radial and ulnar loop
- Open loop vs closed loop in cars
- Rutin för avvikelsehantering
- Vad är vanlig celldelning
- Lågenergihus nyproduktion
- Rådet för byggkompetens
- Myndigheten för delaktighet
- Presentera för publik crossboss
- Var 1721 för stormaktssverige
- Vad är densitet
- Tack för att ni lyssnade bild
- Tack för att ni har lyssnat
- Referatmarkeringar
- Stig karttecken
- Epiteltyper
- Vätsketryck formel
- Vilket tal pekar pilen på