190 likes | 452 Views
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)
E N D
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) statement [block] • Example int temp = 0; while (temp <= 100) { System.out.println (temp); temp += 10; }
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 }
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 }
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
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 }
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);
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
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?
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?
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); }
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
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!
Exiting early from a loop (cont) • a common idiom while (true) { … if (expression1) // some complex test if (expression2) if (expression3) break; }
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();
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; } } }
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; } } }
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; } } }