Скачать книгу

target="_blank" rel="nofollow" href="#ulink_a64d61e8-c4d8-5a75-97ea-0adf67cd8e26">FIGURE 3.9 The structure of a break statement

      Notice in Figure 3.9 that the break statement can take an optional label parameter. Without a label parameter, the break statement will terminate the nearest inner loop it is currently in the process of executing. The optional label parameter allows us to break out of a higher-level outer loop. In the following example, we search for the first (x,y) array index position of a number within an unsorted two-dimensional array:

      When executed, this code will output the following:

      Value 2 found at: (1,1)

      In particular, take a look at the statement break PARENT_LOOP. This statement will break out of the entire loop structure as soon as the first matching value is found. Now, imagine what would happen if we replaced the body of the inner loop with the following:

       if(list[i][j]==searchValue) { positionX = i; positionY = j; break; }

      How would this change our flow, and would the output change? Instead of exiting when the first matching value is found, the program would now only exit the inner loop when the condition was met. In other words, the structure would find the first matching value of the last inner loop to contain the value, resulting in the following output:

      Value 2 found at: (2,0)

      Finally, what if we removed the break altogether?

       if(list[i][j]==searchValue) { positionX = i; positionY = j; }

      In this case, the code would search for the last value in the entire structure that had the matching value. The output would look like this:

      Value 2 found at: (2,1)

      The continue Statement

Schematic illustration of the structure of a continue statement

      You may notice that the syntax of the continue statement mirrors that of the break statement. In fact, the statements are identical in how they are used, but with different results. While the break statement transfers control to the enclosing statement, the continue statement transfers control to the boolean expression that determines if the loop should continue. In other words, it ends the current iteration of the loop. Also, like the break statement, the continue statement is applied to the nearest inner loop under execution, using optional label statements to override this behavior.

      Let's take a look at an example. Imagine we have a zookeeper who is supposed to clean the first leopard in each of four stables but skip stable b entirely.

      With the structure as defined, the loop will return control to the parent loop any time the first value is b or the second value is 2. On the first, third, and fourth executions of the outer loop, the inner loop prints a statement exactly once and then exits on the next inner loop when leopard is 2. On the second execution of the outer loop, the inner loop immediately exits without printing anything since b is encountered right away. The following is printed:

      Cleaning: a,1 Cleaning: c,1 Cleaning: d,1

      Now, imagine we remove the CLEANING label in the continue statement so that control is returned to the inner loop instead of the outer. Line 6 becomes the following:

      6: continue;

      This corresponds to the zookeeper cleaning all leopards except those labeled 2 or in stable b. The output is then the following:

      Cleaning: a,1 Cleaning: a,3 Cleaning: c,1 Cleaning: c,3 Cleaning: d,1 Cleaning: d,3

      Finally, if we remove the continue statement and the associated if statement altogether by removing lines 5–7, we arrive at a structure that outputs all the values, such as this:

      The return Statement

      Given that this book shouldn't be your first foray into programming, we hope you've come across methods that contain return statements. Regardless, we cover how to design and create methods that use them in detail in Chapter 5, “Methods.”

      For now, though, you should be familiar with the idea that creating methods and using return statements can be used as an alternative to using labels and break statements. For example, take a look at this rewrite of our earlier FindInMatrix class:

      public class FindInMatrixUsingReturn { private static int[] searchForValue(int[][] list, int v) { for (int i = 0; i < list.length; i++) { for (int j = 0; j < list[i].length; j++) { if (list[i][j] == v) { return new int[] {i,j}; } } } return null; } public static void main(String[] args) { int[][] list = { { 1, 13 }, { 5, 2 }, { 2, 2 } }; int searchValue = 2; int[] results = searchForValue(list,searchValue); if (results == null) { System.out.println("Value " + searchValue + " not found"); } else { System.out.println("Value " + searchValue + " found at: " + "(" + results[0] + "," + results[1] + ")"); } } }

      This class is functionally the same as the first FindInMatrix class we saw earlier using break. If you need finer-grained control of the loop with multiple break and continue statements, the first class is probably better. That said, we find code without labels and break statements a lot easier to read and debug. Also, making the search logic an independent function makes the code more reusable and the calling main() method a lot easier to read.

      Unreachable Code

      One facet of break, continue, and return that you should be aware of is that any code placed

Скачать книгу