Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The continue statement presented in Section 5.7 proceeds with the next iteration (repetition) of the immediately enclosing while, for or do...while. The labeled continue statement skips the remaining statements in that statement’s body and any number of enclosing repetition statements and proceeds with the next iteration of the enclosing labeled repetition statement (i.e., a for, while or do...while preceded by a label). In labeled while and do...while statements, the program evaluates the loop-continuation test of the labeled loop immediately after the continue statement executes. In a labeled for, the increment expression is executed and the loop-continuation test is evaluated. Figure O.2 uses a labeled continue statement in a nested for to enable execution to continue with the next iteration of the outer for.
|
Code View:
Scroll
/
Show All 1 // Fig. O.2: ContinueLabelTest.java 2 // Labeled continue statement terminating a nested for statement. 3 public class ContinueLabelTest 4 { 5 public static void main( String[] args ) 6 { 7 nextRow: // target label of continue statement 8 9 // count 5 rows 10 for ( int row = 1; row <= 5; row++ ) 11 { 12 System.out.println(); // outputs a newline 13 14 // count 10 columns per row 15 for ( int column = 1; column <= 10; column++ ) 16 { 17 // if column greater than row, start next row 18 if ( column > row ) 19 continue nextRow; // next iteration of labeled loop 20 21 System.out.print( "* " ); 22 } // end inner for 23 } // end outer for 24 25 System.out.println(); // outputs a newline 26 } // end main 27 } // end class ContinueLabelTest
|