A Form of Repetition: The “for-each” Loop#
The for-each loop is used when you want to do the same thing to every item in a structure, over and over with the code, again breaking the normal sequential flow of our code. At the moment you have seen two main structures: Strings and arrays. You can combine these and make arrays of Strings, or arrays of arrays, or arrays of arrays of Strings, etcetera.
Syntax of for-each Loop#
The syntax of the for-each loop is:
for (<data type of item> <item name> : <data structure>) {
\(\;\;\;\;\;\) <statements to repeat, i.e. the loop body>
}
The braces are only required if there are multiple statements to repeat, however, it is a good habit to always include them. Though we might think we will only be writing a single statement in a loop, we often require more and then it is easier to add them. Note that the data type that the programmer specifies is the data type of each item in the structure.
Semantics of for-each Loop#
The meaning of the loop is straight-forward: perform the statements in the loop body, that is, the statements to repeat, on each item of the structure, normally from left to right[1]. An important consideration is that if the loop body changes item in some way, this change is not reflected in the data structure. It is a local change only. The following code might look like it changes an entire array to zeros, but that is not the case, as the array remains in its original state:
int[] anArray = {3, 6, 7, 2, 90, 12, 14, 1, 8};
for (int item : anArray){
item = 0;
}
for (int item : anArray){
System.out.print(item + " ");
}
System.out.println();
3 6 7 2 90 12 14 1 8
Examples of for-each Loop#
Here is an example of moving through an array of Strings to be able to print the sum of the length of all the Strings.
String[] fruitBasket = {"kiwi", "apple", "orange", "watermelon"};
int sumLen = 0;
for (String item : fruitBasket){
sumLen = sumLen + item.length();
}
System.out.println("Sum of all lengths is: " + sumLen);
Sum of all lengths is: 25
Here is an example of finding the index of an item in an array of integers. This algorithm is called sequential search because we move through the array and look at each item in sequence.
int[] myArray = {7, 9, 13, 5, 23, 42, 39, 87, 68, 50};
int itemToFind = 68;
int index = 0; //Starting location in the array
int location = -1; //Use -1 to flag not found
for (int number : myArray){
if (number == itemToFind){
location = index;
}
index++;
}
if (location == -1){
System.out.println("Item NOT found");
}
else{
System.out.println("Item found at location " + location);
}
Item found at location 8
Practice Questions#
Trace the following program, showing the execution path as a sequence of line numbers and providing a memory diagram and the output:
1. int[] n = {10, 11, 15, 13}; 2. for (int value : n){ 3. System.out.print(2 * value + "&"); 4. value = 100; 5. } 6. System.out.println("DONE");
What is
n
in the program of question 1) above.What is
value
in the program of question 1) above.Write Java code, using a for-each loop, to sum the odd numbers between 11 and 29, inclusive.
Write Java code, using a for-each loop, to print the first letter of each item in an array of Strings.
Write Java code, using a for-each loop, to print the last letter of each item in an array of Strings.