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

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

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

Автор: Jeanne Boyarsky

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

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

Серия:

isbn: 9781119864592

isbn:

СКАЧАТЬ and Generics.” For this chapter, you just need to know that on each iteration, a for-each loop assigns a variable with the same type as the generic argument. In this case, name is of type String.

      So far, so good. What about the following examples?

      String birds = "Jay"; for(String bird : birds) // DOES NOT COMPILE System.out.print(bird + " "); String[] sloths = new String[3]; for(int sloth : sloths) // DOES NOT COMPILE System.out.print(sloth + " ");

      The first for-each loop does not compile because String cannot be used on the right side of the statement. While a String may represent a list of characters, it has to actually be an array or implement Iterable. The second example does not compile because the loop type on the left side of the statement is int and doesn't match the expected type of String.

      The final types of control flow structures we cover in this chapter are branching statements. Up to now, we have been dealing with single loops that ended only when their boolean expression evaluated to false. We now show you other ways loops could end, or branch, and you see that the path taken during runtime may not be as straightforward as in the previous examples.

      Nested Loops

      Before we move into branching statements, we need to introduce the concept of nested loops. A nested loop is a loop that contains another loop, including while, do/while, for, and for-each loops. For example, consider the following code that iterates over a two-dimensional array, which is an array that contains other arrays as its members. We cover multidimensional arrays in detail in Chapter 4, “Core APIs,” but for now, assume the following is how you would declare a two-dimensional array:

      int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}}; for(int[] mySimpleArray : myComplexArray) { for(int i=0; i<mySimpleArray.length; i++) { System.out.print(mySimpleArray[i]+"\t"); } System.out.println(); }

      Notice that we intentionally mix a for loop and a for-each loop in this example. The outer loop will execute a total of three times. Each time the outer loop executes, the inner loop is executed four times. When we execute this code, we see the following output:

      5 2 1 3 3 9 8 9 5 7 12 7

      Nested loops can include while and do/while, as shown in this example. See whether you can determine what this code will output:

      int hungryHippopotamus = 8; while(hungryHippopotamus>0) { do { hungryHippopotamus -= 2; } while (hungryHippopotamus>5); hungryHippopotamus--; System.out.print(hungryHippopotamus+", "); }

      On the second iteration of the outer loop, the inner do/while will be executed once, even though hungryHippopotamus is already not greater than 5. As you may recall, do/while statements always execute the body at least once. This will reduce the value to 1, which will be further lowered by the decrement operator in the outer loop to 0. Once the value reaches 0, the outer loop will terminate. The result is that the code will output the following:

      3, 0,

      The examples in the rest of this section include many nested loops. You will also encounter nested loops on the exam, so the more practice you have with them, the more prepared you will be.

      Adding Optional Labels

      One thing we intentionally skipped when we presented if statements, switch statements, and loops is that they can all have optional labels. A label is an optional pointer to the head of a statement that allows the application flow to jump to it or break from it. It is a single identifier that is followed by a colon (:). For example, we can add optional labels to one of the previous examples:

      int[][] myComplexArray = {{5,2,1,3},{3,9,8,9},{5,7,12,7}}; OUTER_LOOP: for(int[] mySimpleArray : myComplexArray) { INNER_LOOP: for(int i=0; i<mySimpleArray.length; i++) { System.out.print(mySimpleArray[i]+"\t"); } System.out.println(); }

      Labels follow the same rules for formatting as identifiers. For readability, they are commonly expressed using uppercase letters in snake_case with underscores between words. When dealing with only one loop, labels do not add any value, but as you learn in the next section, they are extremely useful in nested structures.

      Note Icon While this topic is not on the exam, it is possible to add optional labels to control and block statements. For example, the following is permitted by the compiler, albeit extremely uncommon:

      The break Statement

Schematic illustration of 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 СКАЧАТЬ