ТОП просматриваемых книг сайта:
OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн.Название OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Год выпуска 0
isbn 9781119864592
Автор произведения Jeanne Boyarsky
Жанр Программы
Издательство John Wiley & Sons Limited
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:
public class FindInMatrix { public static void main(String[] args) { int[][] list = {{1,13},{5,2},{2,2}}; int searchValue = 2; int positionX = -1; int positionY = -1; PARENT_LOOP: for(int i=0; i<list.length; i++) { for(int j=0; j<list[i].length; j++) { if(list[i][j]==searchValue) { positionX = i; positionY = j; break PARENT_LOOP; } } } if(positionX==-1 || positionY==-1) { System.out.println("Value "+searchValue+" not found"); } else { System.out.println("Value "+searchValue+" found at: " + "("+positionX+","+positionY+")"); } } }
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)
You can see from this example that using a label on a break
statement in a nested loop, or not using the break
statement at all, can cause the loop structure to behave quite differently.
The continue Statement
Let's now extend our discussion of advanced loop control with the continue statement, a statement that causes flow to finish the execution of the current loop iteration, as shown in Figure 3.10.
FIGURE 3.10 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.
1: public class CleaningSchedule { 2: public static void main(String[] args) { 3: CLEANING: for(char stables = 'a'; stables<='d'; stables++) { 4: for(int leopard = 1; leopard<4; leopard++) { 5: if(stables=='b' || leopard==2) { 6: continue CLEANING; 7: } 8: System.out.println("Cleaning: "+stables+","+leopard); 9: } } } }
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:
Cleaning: a,1 Cleaning: a,2 Cleaning: a,3 Cleaning: b,1 Cleaning: b,2 Cleaning: b,3 Cleaning: c,1 Cleaning: c,2 Cleaning: c,3 Cleaning: d,1 Cleaning: d,2 Cleaning: d,3
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.
For the exam, you will need to know both forms. Just remember that return
statements can be used to exit loops quickly and can lead to more readable code in practice, especially when used with nested loops.
Unreachable Code
One facet of break
, continue
, and return
that you should be aware of is that any code placed