Presentation is loading. Please wait.

Presentation is loading. Please wait.

Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.

Similar presentations


Presentation on theme: "Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated."— Presentation transcript:

1 Summary of Loops Programming

2 COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated a fixed number of times n controlled by a variable that is changed by an equal amount (usually 1) during each iteration l while loop n The number of iterations depends on a condition which could be changed during execution. n Example: working with user input. n In some situations the code segment should not be executed at all. l do-while loop n The code segment is always executed at least once. n Otherwise, the situations when do-while loops are used are very similar to those when while loops are used.

3 COMP102 Prog Fundamentals I: Summary of Loops /Slide 3 How to Stop a Loop Known number of iterations before the loop stops ( for ). l Test for a user-controlled condition before or after each iteration ( while, do-while ). Use the break command.

4 COMP102 Prog Fundamentals I: Summary of Loops /Slide 4 break break is used when we want to terminate a loop before it ends in a normal way. l How it works: When the break statement is executed, the loop statement terminates immediately. n The execution continues with the statement following the loop statement.

5 COMP102 Prog Fundamentals I: Summary of Loops /Slide 5 Maximum ( while with break ) int value=0;// input value int max=0;// maximum value while(true)// infinite loop!!! {cout << "Enter a positive integer " > value; if(value > max) max = value; if(value==-1) break; } cout << "The maximum value found is " << max << endl;

6 COMP102 Prog Fundamentals I: Summary of Loops /Slide 6 Common Loop Errors while(balance != 0.0); { balance = balance - amount; } n This will lead to an infinite loop! for(count=1; count<=number; count++); { cout << "hello" << endl; } n "hello" only printed once!

7 COMP102 Prog Fundamentals I: Summary of Loops /Slide 7 Common Loop Errors double balance = 333.3; double amount = 33.33; int round=0; char tmp; while(balance != 0.0){ balance = balance - amount; cout << round++ <<": the balance is " << balance <<endl; cin >> tmp; } balance may not become equal zero due to numerical inaccuracies.

8 COMP102 Prog Fundamentals I: Summary of Loops /Slide 8 Common Loop Errors int power = 0, n; cout << "Give N :"; cin >> n; while(power <= 1000){ cout<<"Next power of N is ” << power << endl; power *= n; } n Be sure to initialize to 0 a variable used for sums. n Be sure to initialize to 1 a variable used for products.

9 COMP102 Prog Fundamentals I: Summary of Loops /Slide 9 Nested Loops l Nested loops are loops within loops. l Nested loops are similar in principle to nested if and if-else statements. l Many applications require nested loops.

10 COMP102 Prog Fundamentals I: Summary of Loops /Slide 10 Nested Loops // Find the average score on 8 lab assignments int counter, lastlab=8; double avg, score, tscore; char resp; do{ tscore = 0; for(counter =1; counter <=lastlab; counter ++){ cout << "Enter student’s score for lab " << counter << ": "; cin >> score; tscore += score; } avg = tscore/double(lastlab); cout << "The average score is " << avg << endl; cout << "Enter another student (y/n)? "; cin >> resp; }while(resp=='y' || resp=='Y');

11 COMP102 Prog Fundamentals I: Summary of Loops /Slide 11 Multiplication Table // Program to output the // multiplication table int row; // Outer loop counter int col;// Inner loop counter for(row=1; row<=10; row++){ for(col=1; col<=10; col++) cout << row*col << " "; cout << endl; }

12 COMP102 Prog Fundamentals I: Summary of Loops /Slide 12 Zig Zag Pattern l Print out the following ZigZag pattern * * *

13 COMP102 Prog Fundamentals I: Summary of Loops /Slide 13 Zig Zag Pattern l Sub-problem: n print out the upper part n print out the middle part n print out the lower part l Print out upper part: n row 0: print 5 spaces, 3 stars; * * * n row 1: print 4 spaces, 3 stars; * * * n row 2: print 3 spaces, 3 stars; * * * n row 3: print 2 spaces, 3 stars; * * * n row 4: print 1 space, 3 stars; * * * l Print out middle part: n row 5: print 0 space, 3 stars; * * * n row 6: print 1 space, 3 stars; * * * n row 7: print 2 spaces, 3 stars; * * * n row 8: print 3 spaces, 3 stars; * * * n row 9: print 4 spaces, 3 stars; * * * l Print out lower part: n Row 10 - 14 repeat the upper part

14 COMP102 Prog Fundamentals I: Summary of Loops /Slide 14 Zig Zag Pattern l Algorithm for upper part: n row 0: print ( 5-row%10 )spaces, 3 stars; * * * n row 1: print ( 5-row%10 )spaces, 3 stars; * * * n row 2: print ( 5-row%10 )spaces, 3 stars; * * * n row 3: print ( 5-row%10 )spaces, 3 stars; * * * n row 4: print ( 5-row%10 )spaces, 3 stars; * * * l l Algorithm for middle part: n row 5: print ( row%10 - 5 )spaces, 3 stars; * * * n row 6: print ( row%10 - 5 )spaces, 3 stars; * * * n row 7: print ( row%10 - 5)spaces, 3 stars; * * * n row 8: print ( row%10 - 5)spaces, 3 stars; * * * n row 9: print ( row%10 - 5 )spaces, 3 stars; * * * l Algorithm for lower part: n Row 10 - 14: print (5-row%10 ) space, 3 stars;

15 COMP102 Prog Fundamentals I: Summary of Loops /Slide 15 Zig Zag Pattern #include using namespace std; int main() { int space,row,k,j ; for(row = 0; row < 15; row++) { if ( row%10 – 4 <= 0 ) //row 0 ~ 4, 10 ~ 14, … space = 5 - row % 10; else space = row % 10 - 5; //row 5 ~ 9, 15 ~ 19, … for ( k = space; k > 0; k--) //print initial spaces cout << " "; cout << "***" << endl; } return 0; }


Download ppt "Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated."

Similar presentations


Ads by Google