Controlling Execution

Iteration
The comma operator
Comma operator (not the comma separator, which is used to separate definitions and method arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially.
for(int i = 1, j = i + 10; i < 5; i++, j = i * 2) {
      System.out.println("i = " + i + " j = " + j);
}
break and continue
break quits the loop without executing the rest of the statements in the loop. continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.

The infamous “goto”
A label is an identifier followed by a colon, like this:
label1:

The only place a label is useful in Java is right before an iteration statement.
And that means right before—it does no good to put any other statement between the label and the iteration. And the sole reason to put a label before an iteration is if you’re going to nest another iteration or a switch (which you’ll learn about shortly) inside it. That’s because the break and continue keywords will normally interrupt only the current loop, but when used with a label, they’ll interrupt the loops up to where the label exists:
label1:
outer-iteration {
      inner-iteration {
        //...
        break; // (1)
        //...
        continue; // (2)
        //...
        continue label1; // (3)
        //...
        break label1; // (4)
    }
}
In (1), the break breaks out of the inner iteration and you end up in the outer iteration. In (2), the continue moves back to the beginning of the inner iteration.
But in (3), the continue label1 breaks out of the inner iteration and the outer iteration, all the way back to label1. Then it does in fact continue the iteration, but starting at the outer iteration.
In (4), the break label1 also breaks all the way out to label1, but it does not reenter the iteration. It actually does break out of both iterations.

Note that break breaks out of the for loop, and that the increment expression doesn’t occur until the end of the pass through the for loop.

t’s important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level.

switch
The switch statement selects from among pieces of code based on the value of an integral expression.
switch(integral-selector) {
      case integral-value1 : statement; break;
      case integral-value2 : statement; break;
      case integral-value3 : statement; break;
      case integral-value4 : statement; break;
      case integral-value5 : statement; break;
      // ...
      default: statement;
}

The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes. If no match occurs, the default statement executes.

The break is optional. If it is missing, the code for the following case statements executes until a break is encountered.

It requires a selector that evaluates to an integral value, such as int or char. If you want to use, for example, a string or a floating point number as a selector, it won’t work in a switch statement.

猜你喜欢

转载自kellylin1115.iteye.com/blog/2418658