1 / 15

Programming

Programming. Summary of Loops. Which Loop to Use?. for loop 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 while loop

katarina
Télécharger la présentation

Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Summary of Loops

  2. Which Loop to Use? • for loop • 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 • while loop • 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. • do-while loop • 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.

  3. How to Stop a Loop • 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.

  4. break • break is used when we want to terminate a loop before it ends in a normal way. • How it works: • When the break statement is executed, the loop statement terminates immediately. • The execution continues with the statement following the loop statement.

  5. Maximum (while with break) int value=0; // input value int max=0; // maximum value while(true) // 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; MAX

  6. Common Loop Errors while(balance != 0.0); { balance = balance - amount; } • This will lead to an infinite loop! for(count=1; count<=number; count++); { cout << "hello" << endl; } • "hello" only printed once!

  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. 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; } • Be sure to initialize to 0 a variable used for sums. • Be sure to initialize to 1 a variable used for products.

  9. Nested Loops • Nested loops are loops within loops. • Nested loops are similar in principle to nested ifand if-else statements. • Many applications require nested loops.

  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. 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. Zig Zag Pattern • Print out the following ZigZag pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  13. Zig Zag Pattern • Sub-problem: • print out the upper part • print out the middle part • print out the lower part • Print out upper part: • 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; * * * • Print out middle part: • 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; * * * • Print out lower part: • Row 10 - 14 repeat the upper part

  14. Zig Zag Pattern • Algorithm for upper part: • row 0: print ( 5-row%10 )spaces, 3 stars; * * * • row 1: print ( 5-row%10 )spaces, 3 stars; * * * • row 2: print ( 5-row%10 )spaces, 3 stars; * * * • row 3: print ( 5-row%10 )spaces, 3 stars; * * * • row 4: print ( 5-row%10 )spaces, 3 stars; * * * • Algorithm for middle part: • row 5: print ( row%10 - 5 )spaces, 3 stars; * * * • row 6: print ( row%10 - 5 )spaces, 3 stars; * * * • row 7: print ( row%10 - 5)spaces, 3 stars; * * * • row 8: print ( row%10 - 5)spaces, 3 stars; * * * • row 9: print ( row%10 - 5 )spaces, 3 stars; * * * • Algorithm for lower part: • Row 10 - 14: print (5-row%10 ) space, 3 stars;

  15. Zig Zag Pattern #include <iostream> 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; }

More Related