Java Reference
In-Depth Information
3. The next statement opens an enhanced for loop, which will iterate through all the entries of the
array. In a regular for loop, the iterator is generally an integer. In an enhanced for loop, it takes
the same type as the array entries. In this case, it is an integer because the array is an int[ ] . You
use the name of the iterator to refer to the current entry within the loop. In the first iteration, i
takes the value of the first entry of the array, in this case 1. On the next iteration, it will take the
value of the next entry, 2.
4. In the first statement inside the loop, a second int , named doubled , is assigned the value of i* 2 . In
this first iteration, i = 1 , so doubled = 2 .
5. In the next statement of the loop, there is a println command. A line is output to the console that
reads: 1 times two equals 2 . Because the command is println instead of print , you can imag-
ine pressing Enter or Return at the end of the string to create a new line.
6. When the program reaches the end of the loop, it will restart the loop with the next entry in the
array. The iterator i is reassigned the value of the second array entry, in this case 2.
7. The variable doubled is 2*2 or 4 in this iteration of the loop.
8. Another line will be output to the console, this time reading: 2 times two equals 4 .
9. This will continue until every entry in the array has been used. Then, the program will proceed
with the final statement. One final line will be output to the console indicating the end of the
program.
10. If you executed the for loop in the previous Try It Out exercise, you'll notice that they pro-
duce the same output. The two types of for loops work in much the same way, but in some
situations one will be preferable to the other. In this enhanced for loop exercise, it was prob-
ably unnecessary to create an array just to list the integers between 1 and 10; a standard for
loop does this simply by incrementing the iterator by 1 on each loop. For an array of strings or
other objects, iterating through the array in an enhanced for loop may be simpler to code and
easier to read.
Nesting for Loops
As you saw with if-then statements, for loops can also be nested. The format is very similar, where
one inner for loop is contained within another outer for loop. The general appearance is as follows:
for (/*Initialization*/; /*Termination*/; /*Increment*/){ //outer loop
/*execute these statements*/
for (/*Initialization2*/; /*Termination2*/; /*Increment2*/){ //inner loop
/*execute these statements*/
} //close inner loop
} //close outer loop
Because the statements inside the inner for loop can refer to the iterator of both the inner loop and
outer loop, it's necessary to use different variable names in the initialization of each loop. You've
probably noticed that many of the standard for loops shown in this chapter use x as the index
name (and x = 0 as the initialization); this is by no means required, but is often used in practice.
 
Search WWH ::




Custom Search