1 / 19

Iteration

Iteration. Loops. Loops provide a way to repeat a set of instructions multiple times while loops do while loops for loops continue statement break statement. While Loops. Executes the statement [block] while the condition is true Syntax while (boolean expression)

miron
Download Presentation

Iteration

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. Iteration

  2. Loops • Loops provide a way to repeat a set of instructions multiple times • while loops • do while loops • for loops • continue statement • break statement

  3. While Loops • Executes the statement [block] while the condition is true • Syntax while (boolean expression) statement [block] • Example int temp = 0; while (temp <= 100) { System.out.println (temp); temp += 10; }

  4. Loop Structure • Loops have four components: • initialization of tested condition • test • body • change of tested condition • Example int temp = 0; // initialization while (temp <= 100) // test { System.out.println (temp); // body temp += 10; // change }

  5. Loop vs. Test • Loop (repeat) int temp = 0; // initialization while (temp <= 100) // test { System.out.println (temp); // body temp += 10; // change } • Decision (to do or not to do) int temp = 0; // initialization if (temp <= 100) // test { System.out.println (temp); // body temp += 10; // change }

  6. Do While Loops • Executes the statement [block] at least once, and continues while the condition is true • Syntax do statement [block] while (boolean expression); • Example int temp = 0; // initialization do { System.out.println (temp); // body temp += 10; // change } while (temp <= 100); // test at end of loop

  7. For Loops • A shorthand method of looping • Syntax for (initization_stmt; boolean expression to be tested; change_stmt) statement [block] • Equivalent to: initialization_stmt while (boolean_expression) { statement [block] change_stmt }

  8. For Loops (continued) • The while loop: int temp = 0; while (temp <= 100) { System.out.println (temp); temp += 10; } • as a for loop: int temp; for (temp=0; temp<=100; temp+=10) System.out.println (temp);

  9. For Loops (continued) • Variables defined in the for statement are scoped to the body of the loop int sum = 0; for (int j=1; j<=100; j++) sum += j; int x = 2*j; // error, j not available here

  10. For Loops (continued) • initialization & change statements can be multi-part, comma separated for (int j=1, sum=0; j<=100 && sum<1000; j++) { sum += j; } double avg = sum / j; // will this compile? • semicolon at end of for statement indicates no body; the loop behavior is specified in the change stmt for (int j=1, sum=0; j<=100 && sum<1000; sum += j, j++); for (int j=1, sum=0; j<=100 && sum<1000; sum += j++); • what is the value of sum after the above loops?

  11. For Loops -Traps • Absent {}, the body of the loop is one statement; indentation can be misleading int j; double x; for (j=1; j<=100; j++) x = j * Math.PI; System.out.println (j + “ “ + x); • What is printed?

  12. For Loops –Traps (cont.) • unintentional semicolon at end of for statement indicates no body int j; double x; for (j=1; j<=100; j++); { x = j * Math.PI; System.out.println (j + “ “ + x); }

  13. Infinite Loops • The body of the loop (the statement [block]) must change the elements of the boolean expression such that the loop will terminate • In for loops, this is usually handled by the change statement

  14. Exiting early from a loop • Use the break statement to terminate a loop before its primary condition is satisfied // convert Centigrade temperatures to Fahrenheit for (double cent=0.; cent<=100.; cent +=10) { double fahr = Temperature.centToFahr (cent); if (fahr > 100.) break; System.out.println (cent + “ “ + fahr); } • another way double fahr = 0; for(double cent=0.; cent<=100. && fahr<=100.; cent+=10) { fahr = Temperature.centToFahr (cent); System.out.println (cent + “ “ + fahr); } // not quite the same!

  15. Exiting early from a loop (cont) • a common idiom while (true) { … if (expression1) // some complex test if (expression2) if (expression3) break; }

  16. Exiting early from an iteration • Use the continue statement to skip the remainder of an iteration • Example // eliminate whitespace String s = new Scanner().nextLine(); StringBuffer sb = new StringBuffer(); for (int i=0; i<s.length(); i++) { char c = s.charAt (i); if (Character.isWhiteSpace(c)) continue; // more processing sb.append (c); } s = sb.toString();

  17. Nested Loops • Loops may be nested to any level int sum = 0; for (int j=1; j<=10; j++) { for (int k=1; k<=10; k++) { for (int m=1; m<=10; m++) { // how many times is this executed? sum += j + k + m; } } }

  18. Labelled break • Exits the loop identified by the label int sum = 0; outerLoop: for (int j=1; j<=10; j++) { for (int k=1; k<=10; k++) { for (int m=1; m<=10; m++) { sum += j + k + m; if (sum > 10000) break outerLoop; } } }

  19. Labelled continue • Terminates this iteration of the the loop identified by the label int sum = 0; outerLoop: for (int j=1; j<=10; j++) { for (int k=1; k<=10; k++) { if (k == j) continue outerLoop; for (int m=1; m<=10; m++) { sum += j + k + m; } } }

More Related