Effect of the break and continue statements on
Effect of the break and continue statements on the for-statement
Previously discussed. . . • 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
Previously discussed. . . (cont. ) • 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
Effect of the break-statement on a forstatement • According to: • 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 effect is: • The for-statement is terminated • Execution continues with the statement that follows the for-statement
Effect of the break-statement on a forstatement (cont. ) • The execution of the break statement illustrated with a flow chart:
Effect of the continue-statement on a forstatement • According to: • When the continue statement is executed inside a loop -statement, the program will skip over the remainder of the loop-body to the end of the loop body the effect is: • The for-statement skips over the rest of the body. . . because the INCR-STATEMENT is the next statement. . . • Execution continues with the INCR-STATEMENT and then back to the LOOP-CONTINUATION-CONDITION
Effect of the continue-statement on a forstatement (cont. ) • The execution of the continue statement illustrated with a flow chart:
Caveat in using the continue-statement • Warning: • The continue-statement can cause an infinite loop when used inside a while-statement
Caveat in using the continue-statement (cont. ) • Example: print all number 1, 2, . . . , 10, except the number 4 public class Continue 02 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) { if ( a == 4 ) { continue; } // Run a = 1, 2, . . . , 10 // Skip printing 4. . . System. out. println(a); a++; } } } // Print a
Caveat in using the continue-statement (cont. ) This program contains an infinite loop, because the continue statement will skip over a++. • Result: • When a == 4, the variable a is not increased inside the while-body !!! Since variable a does not change, you have an infinite loop
Caveat in using the continue-statement (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/07/Progs/ Continue 02. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Continue 02. java • To run: java Continue 02
Caveat in using the continue-statement (cont. ) • Output: 1 2 3 (program hangs here forever). . .
Caveat in using the continue-statement (cont. ) • On the other hand, a continue statement inside a forstatement will not create an infinite loop: public class Continue 03 { public static void main(String[] args) { int a; for ( a = 1; a <= 10 ; a++ ) // Run a = 1, 2, . . . , 10 { if ( a == 4 ) { continue; // Skip printing 4. . . } System. out. println(a); } } } // Print a
Caveat in using the continue-statement (cont. ) • Output of this program: 1 2 3 (skips over 4 !) 5 6 7 8 9 10
Caveat in using the continue-statement (cont. ) • Reason: • The continue statement will cause the exeution to jump to the INCR-STATEMENT (a++) • The variable a is increased. . . So no infinite loop
Caveat in using the continue-statement (cont. ) • Example Program: (Demo above code) – Prog file: http: //mathcs. emory. edu/~cheung/Courses/170/Syllabus/07/Progs/ Continue 03. java • How to run the program: • Right click on link and save in a scratch directory • To compile: javac Continue 03. java • To run: java Continue 03
- Slides: 16