break and continue Statements

In addition to selection and repetition statements, Java provides statements break and continue (presented in this section and Appendix K, Labeled break and continue Statements) to alter the flow of control. The preceding section showed how break can be used to terminate a switch statement's execution. This section discusses how to use break in a repetition statement.

In addition to the break and continue statements discussed in this section, Java provides the labeled break and continue statements for use in cases in which a programmer needs to conveniently alter the flow of control in nested control statements. We discuss the labeled break and continue statements in Appendix K.

break Statement

The break statement, when executed in a while, for, do...while or switch, causes immediate exit from that statement. Execution continues with the first statement after the control statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch (as in Fig. 5.9). Figure 5.12 demonstrates a break statement exiting a for.

Figure 5.12. break statement exiting a for statement.

(This item is displayed on page 201 in the print version)

1 // Fig. 5.12: BreakTest.java 2 // break statement exiting a for statement. 3 public class BreakTest 4 { 5 public static void main( String args[] ) 6 { 7 int count; // control variable also used after loop terminates 8 9 for ( count = 1; count <= 10; count++ ) // loop 10 times 10 { 11 if ( count == 5 ) // if count is 5, 12 break; // terminate loop 13 14 System.out.printf( "%d ", count ); 15 } // end for 16 17 System.out.printf( " Broke out of loop at count = %d ", count ); 18 } // end main 19 } // end class BreakTest  

1 2 3 4 Broke out of loop at count = 5  

When the if nested at line 11 in the for statement (lines 915) detects that count is 5, the break statement at line 12 executes. This terminates the for statement, and the program proceeds to line 17 (immediately after the for statement), which displays a message indicating the value of the control variable when the loop terminated. The loop fully executes its body only four times instead of 10.

continue Statement

The continue statement, when executed in a while, for or do...while, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In while and do...while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.

Figure 5.13 uses the continue statement in a for to skip the statement at line 12 when the nested if (line 9) determines that the value of count is 5. When the continue statement executes, program control continues with the increment of the control variable in the for statement (line 7).

Figure 5.13. continue statement terminating an iteration of a for statement.

(This item is displayed on pages 201 - 202 in the print version)

1 // Fig. 5.13: ContinueTest.java 2 // continue statement terminating an iteration of a for statement. 3 public class ContinueTest 4 { 5 public static void main( String args[] ) 6 { 7 for ( int count = 1; count <= 10; count++ ) // loop 10 times 8 { 9 if ( count == 5 ) // if count is 5, 10 continue; // skip remaining code in loop 11 12 System.out.printf( "%d ", count ); 13 } // end for 14 15 System.out.println( " Used continue to skip printing 5" ); 16 } // end main 17 } // end class ContinueTest  

1 2 3 4 6 7 8 9 10 Used continue to skip printing 5  

In Section 5.3, we stated that while could be used in most cases in place of for. The one exception occurs when the increment expression in the while follows a continue statement. In this case, the increment does not execute before the program evaluates the repetition- continuation condition, so the while does not execute in the same manner as the for.

Software Engineering Observation 5.3

Some programmers feel that break and continue violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use break or continue.

Software Engineering Observation 5.4

There is a tension between achieving quality software engineering and achieving the best-performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

Категории