Jump Statements

   

In addition to the more common control flow functions, Java also has three kinds of jump statements: break, continue, and return.

The break and continue statements optionally include a label to identify their target statement. A label is any valid identifier prefixed to a statement or block of statements as shown in the following:

 myLabelName: statement; 

Java does not support a goto operator, but you can use labels to direct execution within nested loops using a break or continue. As you'll see in the next sections, labels are meant to allow you to specify which loop you are referencing with one of these jump statements. They do not, however, give you the ability to jump anywhere in your code like the much maligned goto does.

Note

In some respects, you can view the throw statement used to raise an exception as a jump statement because it causes an abrupt transfer of control. However, this statement is intended to handle error conditions rather than anticipated processing like that associated with the statements discussed in this section. You should never throw an exception (or intentionally perform an operation that causes one to be thrown) to direct program flow when processing is proceeding normally. Exception throwing is an expensive operation in terms of performance that should be reserved for error handling. See Chapter 4, "Methods and Exceptions," for a description of throw.


break Statements

The substatement (or inner) blocks of loops and switch statements can be terminated using the break statement. You will often find situations where a certain condition occurs that makes it unnecessary or undesirable to continue executing a loop or a switch statement case. You have seen the importance of break with a switch statement already. An unlabeled break statement passes control to the next line after the current (innermost) iteration ( while, do, for, or switch statement). Consider the example in Listing 6.3.

Listing 6.3 BreakTest.java ”Using break Inside a while Loop
 public class BreakTest {   public static void main(String[] args) {     try {       // loop forever until an x is typed       while (true) {         int c = System.in.read();         if (c == 'x')           break;       }     }     catch (java.io.IOException e) {       e.printStackTrace();     }     finally {       System.out.println("Will do this before leaving the loop");     }     System.out.println("Started here after break was called");   } } 

In this example, the while loop iterates on a condition that will never be false, but a break statement provides an escape when the character x is read from the keyboard. Because the break is inside a try block, the finally block is executed before control transfers to the last print statement. It is possible for a finally block to redirect control by throwing an exception or executing a return statement.

See "Catching and Throwing Exceptions," for a discussion of try-catch-finally

You can include a label as part of a break statement to pass control to a labeled statement within the current method. This is typically used with nested loops as shown in the following example:

 outerLoop: for (int i=0; i<10; i++) {   System.out.println("Iterate outer loop");   innerLoop: while (true) {     System.out.println("Iterate inner loop");     int c = System.in.read();     if (c == 'x')       break;  // exit while loop and iterate for loop     else if (c == 'q')       break outerLoop;  // exit for loop   } } 

continue Statements

Instead of leaving a loop, a continue statement requests that the remaining statements in an iteration be skipped and that the loop start its next iteration. This is typically a replacement for one or more if statements that would otherwise be required to prevent execution of some statements during an iteration. A continue may only appear within the substatement block of a while, do, or for statement. As with break, the optional label parameter allows you to end an iteration of an outer loop if necessary.

If there is a finally clause for a currently open try statement within the indicated level of nesting, that clause is executed before control is transferred due to a continue.

The following example demonstrates the use of the continue statement:

 int[] data = { 2,0,5,1,0,3} ; for (int i=0; i<data.length; i++) {   if (data[i]==0)     continue;   // now do processing that is only necessary for non-zero data  } 

return Statements

A return statement passes control to the caller of the method, constructor, or static initializer containing the return statement. Unless the return statement is in a class constructor or a method that is declared void, it must include a parameter of the same type specified in the method declaration.

If a return statement is encountered inside a try block, the finally clause associated with the block, if there is one, will be executed before control is transferred.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net