OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Чтение книги онлайн.

Читать онлайн книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky страница 70

Название: OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Автор: Jeanne Boyarsky

Издательство: John Wiley & Sons Limited

Жанр: Программы

Серия:

isbn: 9781119864592

isbn:

СКАЧАТЬ 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 immediately after them in the same block is considered unreachable and will not compile. For example, the following code snippet does not compile:

      int checkDate = 0; while(checkDate<10) { checkDate++; if(checkDate>100) { break; checkDate++; // DOES NOT COMPILE } }

      Even though it is not logically possible for the if statement to evaluate to true in this code sample, the compiler notices that you have statements immediately following the break and will fail to compile with “unreachable code” as the reason. The same is true for continue and return statements, as shown in the following two examples:

      int minute = 1; WATCH: while(minute>2) { if(minute++>2) { continue WATCH; System.out.print(minute); // DOES NOT COMPILE } } int hour = 2; switch(hour) { case 1: return; hour++; // DOES NOT COMPILE case 2: }

      One thing to remember is that it does not matter if the loop or decision structure СКАЧАТЬ