Chapter 16. Final Preparations / For-each Loops
There’s nothing left, we escape next Monday. Today is Tuesday, so we have enough time to prepare everything. Tomorrow Chani’s cousin will come and I’ll give him all the information. On the other hand, I need to explain to him exactly where we’ll meet. I also need to tell Phil, since he’ll be the one picking me up to bring me back to prison without anyone noticing.
I’ve had plenty of time to choose the location and, without a doubt, I think it’s the perfect place. Once we escape, we’ll go to a house owned by Pedro’s friend. That’s where we’ll separate, since everyone has their own plan. Near that house there’s a small public library that almost nobody uses. It’s possible to reserve rooms, so Chani can even hide some microphones in advance to record the conversation.

There’s a small parking lot nearby where there are always free spaces. That’s where I’ll meet Phil. It’s located in a somewhat marginal neighborhood, so it’s very unlikely there are cameras. Even so, no one will be looking for me, since everyone will assume I’m still in prison. Nevertheless, I like to be cautious, something I’ve learned from Bud and Pedro.
It’s important that my voice doesn’t appear in the recording that will incriminate Rich. Otherwise, the authorities would find out that I left prison and I would be in trouble. Chani could delete the parts where I appear, but an edited audio would lose credibility. Therefore, once I enter the library room, I must remain silent. For it not to seem strange, it’s vital that I greet Rich outside the library. I’ll be able to have a completely natural conversation and then, once inside the room, let the two of them talk.
I have everything well tied up, but I hope there aren’t many unforeseen events. The truth is that I’m worried about not being able to control what happens outside the prison. Here it’s simpler and, besides, I have help. Outside I’ll be alone; I’ll have Chani and Phil’s support, but it’s not the same. I don’t trust them as much as I trust my fellow inmates.

Tonight I’ll continue with my Java classes. I’m sure I’ll get a good grade on my next university exam. It seems we’re going to cover a type of loop that we didn’t see when we addressed the topic earlier. The truth is that I quite liked the loop exercises, so I imagine this topic will also be entertaining and easy to understand.
Advantages of for-each Loops
Several topics ago, we covered the concept of loops and worked in depth with for and while loops. Now we’re going to focus on another type, which I intentionally left aside at the time. Even focusing on just two, it’s a somewhat dense concept and a complicated topic. But your Programming level is much higher now so you can handle this and more.
For-each loops are a fundamental tool due to their simplicity and efficiency when iterating over collections and arrays. The main advantages of using this type of loops are:
- Simplicity and better readability: The syntax is clearer and more concise than traditional loops (for or while). This makes the code easier to understand.
- Error reduction: By eliminating the need to handle indices manually, it reduces the risk of errors such as index overflow (accessing a position outside the array range).
- Convenience: It’s especially useful for iterating over collections of objects, like lists and sets, where indices are not as relevant.
But not everything is advantageous, otherwise we would only use this type of loops, so let’s look at the disadvantages and situations where it’s better not to use them.
- Limitation when working with indices: You can’t directly access the current element’s index. If you need the index, you must use a traditional for loop.
- Can’t modify the collection: If you need to add or remove elements from the collection while iterating, a for-each loop is not suitable.
- Forward iteration only: The for-each loop only allows forward iteration, so you can’t traverse the collection in reverse.
- Limitations in multidimensional Arrays: This is basically because in these cases it’s somewhat more complicated and less intuitive to use for-each compared to traditional loops.
For-each Loop Syntax
The basic syntax of the for-each loop in Java is:
for (ElementType temporaryVariable : array) {
// Code that uses temporaryVariable
}
The syntax is simple: first we see the data type and then the temporary variable. Basically, this will take a different value each time the loop makes an iteration. Finally, we put the array that will be traversed. Let’s see it with an example with real data:
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int number : numbers) {
System.out.println(number);
}
}
}
In this example:
- int is the type of elements in the “numbers” array.
- “number” is the temporary variable that takes the value of each element in “numbers”.
- “numbers” is the array being iterated.
Now let’s see an example with a text array:
public class Main {
public static void main(String[] args) {
String[] fruits = {"Apple", "Orange", "Banana"};
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
In this example:
- String is the type of elements in the fruits array.
- fruit is the temporary variable that takes the value of each element in fruits.
- fruits is the array being iterated.
Since this topic has been very short and a continuation of the loops topic, we won’t make a summary of it. Doing so would simply be repeating the same thing again. As you have observed, the syntax is very simple. Although to make it stick in your head, and gain confidence, I recommend that you do all the exercises I propose below.ntinuación del tema de bucles, no vamos a hacer un resumen del mismo. Hacerlo sería simplemente repetir lo mismo otra vez. Cómo has podido observar, la sintaxis es muy sencilla. Aunque para que se te grabe en la cabeza, y ganes confianza, te recomiendo que hagas todos los ejercicios que te propongo a continuación.