1 / 23

More loops

More loops. Linag, Chpt 3, pp 82-99. loop-body statements. false. continue-condition?. true. true. continue-condition?. loop-body statements. false. next statement. next statement. The do -loop. DO-LOOP. WHILE-LOOP. Using a do -loop.

baker-orr
Download Presentation

More loops

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. More loops Linag, Chpt 3, pp 82-99

  2. loop-body statements false continue-condition? true true continue-condition? loop-body statements false next statement next statement The do-loop DO-LOOP WHILE-LOOP

  3. Using a do-loop Use this form when you want the loop body to be executed at least once int data; int sum = 0; String dataString; do { dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); sum += data; } while (data != 0); JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);

  4. Things to know about do-loops • The loop body will always be executed one • The first execution is done before the guard is checked • The do-loop is nothing more than a small variant of the while-loop • It is often useful for checking user input

  5. When to use the do-loop? int sum; int data; String dataString; dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); while(data != 0) { dataString=JOptionPane.showInputMessage(null,”enter number:”); data = Integer.parseInt(dataString); sum = sum + data; //could have used: sum += data; } JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum);

  6. A better way int data; int sum = 0; String dataString; do { dataString=JOptionPane.showInputDialog(null,”enter number:”); data = Integer.parseInt(dataString); sum += data; } while (data != 0); JOptionPane.showMessageDialog(null,”Numbers sum to ”+sum); We don’t have to get the first piece of data, add it to the sum variable, and then start the loop.

  7. The for-loop • The for-loop is another variant of the while loop • Every for-loop can be written as a while loop • Not every while-loop can be written as a for-loop • Use a for-loop when you know exactly how many times the loop body should be executed

  8. The for loop: Three things in one line for ( <initialization>; <guard>; <adjustment>) { ... } • Initialization: (declare and) set a counter, to 0. This is done before everything else. • Guard: loop ends when this is false. This is tested before each pass through the loop. • Adjustment: e.g. increment a counter. This is done at the end of each pass through the loop.

  9. Examples for (int i=0; i<10; i++) { JOptionPane.showMessageDialog(null, “ ”+i); } int i; for (i=10; i>0; i--) { JOptionPane.showMessageDialog(null, “ ”+i); }

  10. From while to for int i = startValue; while (i < endValue) { ..... i++; } for (int i=startValue; i<endValue; i++) { ... }

  11. adjust test guard loop body next statement Flow of control in a for-loop initialization false true

  12. Rules and guidelines for “for” • Counters can be declared and initialized in one go • Never (never) change the value of the counter inside the loop body • I mean it. Never do that! • If the loop body is one statement only, you can omit the braces—but please don’t! • Indent the code within the loop body..

  13. Examples // Compute sum = 0.01 + 0.02 + … + 1; ... double sum = 0; // Keep adding 0.01 to sum for (double i=0.01; i <= 1.0 ; i = i+0.01) { sum += i; } JOptionPane.showMessageDialog("The sum is " + sum);

  14. Common error • The 3 elements of the for-loop header are sparated by semi-colons, not commas! • Do this: • for (int i=0; i<10; i++) • Not this: • for (int i=0, i<10, i++) • Keep your loops as simple as possible

  15. Nesting for-loops • Inside the loop body of a for-loop, we can put another for-loop • Each time through the 1stfor-loop, we execute the 2nd loop until its guard is false • Handy for printing tables like this: 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4

  16. Simple example for (int i=0; i<5; i++) { for (int j=0; j<3; j++) { System.out.print(i+“ ”); } System.out.println(); }

  17. Sample problem, p. 91 Multiplication table --------------------------------- | 1 2 3 4 5 6 7 8 9 1 | 1 2 3 4 5 6 7 8 9 2 | 2 4 6 8 10 12 14 16 18 3 | 3 6 9 12 15 18 21 24 27 4 | 4 8 12 16 20 24 28 32 36 5 | 5 10 15 20 25 30 35 40 45

  18. Nested for-loop example p. 91 // Print table body for (int i=1; i<=9; i++) { output += i + " | "; for (int j=1; j<=9; j++) { // Display the product and align properly if (i*j < 10) output += " " + i*j; else output += " " + i*j; } output += “\n”; // the special character “\n” // represents the end of a line JOptionPane.showMessageDialog(null, output); }

  19. break-ing out of a loop • Inside a loop body, the statement break; causes execution to leave the loop immediately. • This is usually unnecessary, and shows that not enough though has gone into the development of a guard.

  20. Example of break int sum = 0; int item = 0; while (item < 5) { item ++; sum += item; if (sum >= 6) break; } System.out.println("The sum is " + sum);

  21. continue • This keyword also stops interation of the loop body • But...the program then starts the next iteration of the loop (testing the loop guard first) • This is also usually unnecessary, and can be replaced by an if-statement.

  22. Example of continue int sum = 0; int item = 0; while (item < 5) { item++; if (item == 2) continue; sum += item; } System.out.println("The sum is " + sum);

  23. Case studies • Study cases 3.7, 3.8 and 3.9, • Liang, pp 99..106 • Compile and run the programs • Study the logic of flow control • Then go outside and do something else

More Related