|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
break and continue StatementsIn addition to the if, switch, while, and do-while control-flow constructs, Java supports two additional statements: break and continue. These are considered jump statements, because they allow the flow of program execution to branch out in directions not seen with the standard control-flow statements already discussed. As youve seen, the switch statement uses the break statement to terminate a cases execution. However, those break statements were used without labels. Both break and continue can be used with an optional label, specifying exactly where the execution will be transferred. Without a label, break and continue behave exactly as they do in C. Take a look at the following example of a labeled break statement in a switch occurring in a while loop: int x = 0; enterLoop: while( x++ < 10) { System.out.println( "Inside the while loop, iteration:" + x); switch( x) { case 0: System.out.println( "Inside switch, x:" + x); break; case 1: System.out.println( "Inside switch, x:" + x); break; case 2: System.out.println( "Inside switch, x:" + x); break; default: if( x == 5) { System.out.println("Break out of switch and while."); break enterLoop; } break; } System.out.println( "Out of switch, back in while loop."); } Each time through the while loop, the switch statement is encountered. Up until the time x is equal to 5, standard break statements are used to break out of the switch statement and back into the while loop. However, when x is 5, the line is executed: break enterLoop; When this happens, the break occurs not only for the switch statement, but also for the entire while loop! If this labeled break were not present, the while loop would execute 10 times. However, it executes only 5 times, because the labeled break kicks the flow of control out of both the switch and the while loop. Whereas the break statement is used to break out of the loop, a labeled continue statement redirects the flow to the label itself. Unlike break, the labeled continue statement transfers control of program execution to the iteration following the label: int x = 0; enterLoop: while( x++ < 5) { System.out.println( "Inside the while loop, x: " + x); for( int i=0; i<10; i++) { System.out.println( "Inside for loop, i: " + i); if( i == 5) { System.out.println( "Out of for loop"); continue enterLoop; } } System.out.println( "Out of for loop, back in while."); } Here, Ive created a for loop inside a while loop. Each time through the while loop, the for loop is executed until i equals 5, at which point program execution jumps out of the for loop and goes to the first statement inside the while loop: System.out.println("Inside while loop, iteration: " + x); When this happens, the final output line in the while loop isnt executed, because the flow of execution has been rerouted to its beginning. However, if I hadnt included a label, the break statement alone would have rerouted the execution to the first line of code following the for loop. In that case, the final output line would have been executed. In Java, there are actually four jump statements:
The return statement is used to return program execution to the caller of a code segmentfor example, when a method has been invoked. At any point in the method, a return statement can return the flow of control to the caller of that method. The throw statement is used to signal a runtime exception, described in the next section, which interrupts the flow of program execution while a handler is sought to deal with that exception.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |