Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lecture 14: for loops, break, continue and Which loop to use? Feb 13 2015 Example: Syntax: or (initial-action; loopint i; for loops continuation-condition; for (i = 0; i < 100; i++) { Syntax: Example: for (initial-action; action-after-each-iteration) { System.out.println( int i; loopcontinuation-condition; // loop body; for (i=0; i < 100; i++) { action-after-each-iteration) "Welcome to Java!"); { System.out.println Statement(s); ("Welcome to Java"); // loop body; } } } Statement(s); } Initial-Action Loop Continuation Condition? i=0 false (i < 100)? true Statement(s) (loop body) true System.out.println( "Welcome to Java"); Action-After-Each-Iteration i++ (A) (B) false 8 Note on for loops • The initial-action in a for loop can be a list of zero or more comma-separated expressions. • The action-after-each iteration in a for loop can be a list of zero or more comma separated statements. Therefore, the following two for loops are correct. They are rarely used in practice, however. for (int i = 1; i < 100; System.out.println(i++)); for (int i = 0, j = 0; (i + j < 10); i++, j++) { // Do something } Caution 1Note on for loops If the loop-continuation-condition in ainfor looploop is omitted, If the loop-continuation-condition a for is it isomitted, implicitly Thus the statement given below in (a), it istrue. implicitly true. Thus the statement which is below an infinite loop, is correct. Nevertheless, given in (a), which is an infinite loop, is it is better to use the equivalent itloop in (b)totouse avoid “correct”. Nevertheless, is better theconfusion: equivalent loop in (b) to avoid confusion: !"#$%$&$&$'$($ $$))$*"$+",-./012$ 3$ $$ (a) Equivalent 4/05-$%.#6-'$($ $$))$*"$+",-./012$ 3$$ $ (b) $$ 20 Caution 2 on for loops Adding a semicolon at the end of the for clause before the loop body is a common logic error: for (int i=0; i<10; i++); { System.out.println("i is " + i); } Semicolons and loops Similarly, the following loop is also wrong: int i=0; while (i < 10);{//Logic error System.out.println("i is " + i); i++; } In the case of the do-while loop, the semicolon is needed to end the loop. int i=0; do { System.out.println("i is " + i); i++; } while (i<10);//Correct break and continue statements The break statement: When the break statement is executed inside a loopstatement, the loop-statement is terminated immediately The execution of the program will continue with the statement following the loop-statement The continue statement When the continue statement is executed inside a loopstatement, the program will skip over the remainder of the loop-body to the end of the loop body, without exiting the loop completely break and continue statements • while loops • do-while loops • For loops Which Loop to Use? The three forms Which of loop statements, while, do-while, and for, are loop to use? expressively equivalent; that is, you can write a loop in any of these Which Use? three forms. For example, a Loop while loopto in (a) in the following figure canwhile, always be converted into theand following forare loop in (b): do-while, for, The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, write expressively equivalent; that is, you can write ayou loopcan in any of these :2&*1$%*"".+,"'(&'/)(&"'+,"'0&(&"'3$4$ !"#$%$-$*"".+,"'(&'/)(&"'+,"'0&(&"'-$3$4$ Equivalent $$55$6"".$7"08$ $$55$6"".$7"08$ threea forms. For example, a whilethree loop in9$ (a) in theExamples: following figure 9$ loop in any of these forms. $ $ (b) always be(a)converted into the following $can $ $$ for loop in (b): :2&*1$%*"".+,"'(&'/)(&"'+,"'0&(&"'3$4$ Equivalent !"#$%$-$*"".+,"'(&'/)(&"'+,"'0&(&"'-$3$4$ A for loop in (a) in the following figure can generally be converted into the $$55$6"".$7"08$ $$55$6"".$7"08$ 9$ 9$ following while loop in (b) except in certain special cases (see Review Question $ $ (a) (b) $$ 3.19 for one of them): $$ !"#$%&'&(&)*+),(&"'-$$ &'&(&)*+),(&"'-$$ $$$$$*"".+,"'(&'/)(&"'+,"'0&(&"'-$$ :2&*1$%*"".+,"'(&'/)(&"'+,"'0&(&"'3$4$$ A for loop in (a) in the following figure can generally be converted into the Equivalent $$$$$),(&"'+)!(1#+1),2+&(1#)(&"'3$4$ $$55$6"".$7"08-$ following while loop in (b) except in certain special cases (see Review Question $$55$6"".$7"08-$ $$),(&"'+)!(1#+1),2+&(1#)(&"'-$ 9$ 9$ 3.19 for one of them): $ $ (a) (b) $ $$ !"#$%&'&(&)*+),(&"'-$$ &'&(&)*+),(&"'-$$ 23 $$$$$*"".+,"'(&'/)(&"'+,"'0&(&"'-$$ :2&*1$%*"".+,"'(&'/)(&"'+,"'0&(&"'3$4$$ Equivalent $$$$$),(&"'+)!(1#+1),2+&(1#)(&"'3$4$ $$55$6"".$7"08-$ $$55$6"".$7"08-$ $$),(&"'+)!(1#+1),2+&(1#)(&"'-$ 9$ 9$ $ $ (a) (b) Which loop to use? Use the one that is most intuitive and comfortable for you. • In general, a for loop may be used if the number of repetitions is known, as, for example, when you need to print a message 100 times. • A while loop may be used if the number of repetitions is not known, as in the case of reading the numbers until the input is 0. • A do-while loop can be used to replace a while loop if the loop body has to be executed before testing the continuation condition.