# loop - Programming Summary of Loops Prog Fundamentals I:...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Programming Summary of Loops Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? q for loop s s q while loop s s s for calculations that are repeated a fixed number of times controlled by a variable that is changed by an equal amount (usually 1) during each iteration The number of iterations depends on a condition which could be changed during execution. Example: working with user input. In some situations the code segment should not be executed at all. The code segment is always executed at least once. Otherwise, the situations when do-while loops are used are very similar to those when while loops are used. q do-while loop s s Prog Fundamentals I: Summary of Loops /Slide 3 How to Stop a Loop q Known number of iterations before the loop stops (for). Test for a user-controlled condition before or after each iteration (while, do-while). Use the break command. q q Prog Fundamentals I: Summary of Loops /Slide 4 break q break is used when we want to terminate a loop before it ends in a normal way. How it works: s When the break statement is executed, the loop statement terminates immediately. s The execution continues with the statement following the loop statement. q Prog Fundamentals I: Summary of Loops /Slide 5 Maximum (while with break) int value=0; int max=0; while(true) { // input value // maximum value // infinite loop!!! cout << "Enter a positive integer " << "(-1 to stop):"; cin >> value; if(value > max) max = value; if(value==-1) break; } cout << "The maximum value found is " << max << endl; Prog Fundamentals I: Summary of Loops /Slide 6 Common Loop Errors while(balance != 0.0); { balance = balance - amount; } s This will lead to an infinite loop! for(count=1; count<=number; count+ +); { cout << "hello" << endl; } 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; } 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; } s s Be sure to initialize to 0 a variable used for sums. Be sure to initialize to 1 a variable used for products. Prog Fundamentals I: Summary of Loops /Slide 9 Nested Loops q q Nested loops are loops within loops. Nested loops are similar in principle to nested if and if-else statements. Many applications require nested loops. q 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'); 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; } Prog Fundamentals I: Summary of Loops /Slide 12 Zig Zag Pattern q Print out the following ZigZag pattern *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** Prog Fundamentals I: Summary of Loops /Slide 13 Zig Zag Pattern q Sub-problem: print out the upper part s print out the middle part s print out the lower part s q Print out upper part: s s s s s row 0: print 5 spaces, 3 stars; row 1: print 4 spaces, 3 stars; row 2: print 3 spaces, 3 stars; row 3: print 2 spaces, 3 stars; row 4: print 1 space, 3 stars; row 5: print 0 space, 3 stars; row 6: print 1 space, 3 stars; row 7: print 2 spaces, 3 stars; row 8: print 3 spaces, 3 stars; row 9: print 4 spaces, 3 stars; Row 10 - 14 repeat the upper part *** *** *** *** *** *** *** *** *** *** q Print out middle part: s s s s s q Print out lower part: s Prog Fundamentals I: Summary of Loops /Slide 14 Zig Zag Pattern q Algorithm for upper part: s s s s s row 0: print ( 5-row%5 )spaces, 3 stars; row 1: print ( 5-row%5 )spaces, 3 stars; row 2: print ( 5-row%5 )spaces, 3 stars; row 3: print ( 5-row%5 )spaces, 3 stars; row 4: print ( 5-row%5 )spaces, 3 stars; *** *** *** *** *** q Algorithm for middle part: s s s s s row 5: print ( row%5 )spaces, 3 stars; row 6: print ( row%5 )spaces, 3 stars; row 7: print ( row%5 )spaces, 3 stars; row 8: print ( row%5 )spaces, 3 stars; row 9: print ( row%5 )spaces, 3 stars; *** *** *** *** *** q Algorithm for lower part: s Row 10 - 14: print (5-row%5 ) space, 3 stars; Prog Fundamentals I: Summary of Loops /Slide 15 Zig Zag Pattern #include <iostream.h> 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 % 5; else space = row % 5; //row 5 ~ 9, 15 ~ 19 , … for ( k = space; k > 0; k--) cout << " "; cout << "***" << endl; } return 0; } //print initial spaces ...
View Full Document

## This note was uploaded on 09/30/2010 for the course COMP COMP102 taught by Professor Lrr during the Fall '09 term at HKUST.

Ask a homework question - tutors are online