Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 202
Working with for Loops
ОглавлениеAlthough most for
loops you are likely to encounter in your professional development experience will be well defined and similar to the previous examples, there are a number of variations and edge cases you could see on the exam. You should familiarize yourself with the following five examples; variations of these are likely to be seen on the exam.
Let's tackle some examples for illustrative purposes:
1 Creating an Infinite Loop for( ; ; ) System.out.println("Hello World");Although this for loop may look like it does not compile, it will in fact compile and run without issue. It is actually an infinite loop that will print the same statement repeatedly. This example reinforces the fact that the components of the for loop are each optional. Note that the semicolons separating the three sections are required, as for( ) without any semicolons will not compile.
2 Adding Multiple Terms to the for Statement int x = 0; for(long y = 0, z = 4; x < 5 && y < 10; x++, y++) { System.out.print(y + " "); } System.out.print(x + " ");This code demonstrates three variations of the for loop you may not have seen. First, you can declare a variable, such as x in this example, before the loop begins and use it after it completes. Second, your initialization block, boolean expression, and update statements can include extra variables that may or may not reference each other. For example, z is defined in the initialization block and is never used. Finally, the update statement can modify multiple variables. This code will print the following when executed:0 1 2 3 4 5
3 Redeclaring a Variable in the Initialization Block int x = 0; for(int x = 4; x < 5; x++) // DOES NOT COMPILE System.out.print(x + " ");This example looks similar to the previous one, but it does not compile because of the initialization block. The difference is that x is repeated in the initialization block after already being declared before the loop, resulting in the compiler stopping because of a duplicate variable declaration. We can fix this loop by removing the declaration of x from the for loop as follows:int x = 0; for(x = 0; x < 5; x++) System.out.print(x + " ");Note that this variation will now compile because the initialization block simply assigns a value to x and does not declare it.
4 Using Incompatible Data Types in the Initialization Block int x = 0; for(long y = 0, int z = 4; x < 5; x++) // DOES NOT COMPILE System.out.print(y + " ");Like the third example, this code will not compile, although this time for a different reason. The variables in the initialization block must all be of the same type. In the multiple-terms example, y and z were both long, so the code compiled without issue; but in this example, they have different types, so the code will not compile.
5 Using Loop Variables Outside the Loop for(long y = 0, x = 4; x < 5 && y < 10; x++, y++) System.out.print(y + " "); System.out.print(x); // DOES NOT COMPILEWe covered this already at the start of this section, but it is so important for passing the exam that we discuss it again here. If you notice, x is defined in the initialization block of the loop and then used after the loop terminates. Since x was only scoped for the loop, using it outside the loop will cause a compiler error.