4 ways to make a for loop in Java
Writed by
on .
java
planeta-codigo
programacion
Permanent link
Comments
The usual way in Java to do a loop is with a for or while statement but with the addition of iterators in Java 5 it is not necessary to have a variable to keep the index of the loop. Since Java 8, streams have been added that offer other new ways of iterating over the elements of a collection, in the latter case with techniques of functional languages.
Until Java 5 to make a loop from 0 to N elements you had to use a variable to keep a counter, do a comparison to check if the limit had been reached and increase the variable in the next execution. The code was quite verbose and since loops are a basic construction of any programming language it is used many times in any algorithm.
Some of these examples of loops are usable from Java 5, in more recent versions many other new features have been added such as lambdas, streams, methods in interfaces and new API for dates in Java 8, the modularity, improved try-with-resource, jlink or a new publishing model in Java 9, type inference for local variables in Java 10, an HTTP client in Java 11 and other novelties in the Java language and platform.
Loops are one of Java’s basic flow control structures and statement types that allow repeating the execution of a block of statements as long as the repetition condition expression is met, in each iteration of the loop the condition expression is evaluated and when it is not fulfilled, it continues with the next sentence of the program.
Article content
for loop
Before Java 5, a for loop from 0 to 5 and a collection was done in the following way, maintaining a variable normally named i that acts as a counter and j if the for loop is nested in another. In addition to the counter variable, it requires setting the condition that allows to exit the loop when the end of the iteration is reached, the condition is very important not to create an infinite loop.
|
|
|
|
foreach loop
In Java 5 the for loop was significantly enriched, the foreach loop is an improved for loop with which you can loop through a collection and any object that implements the Iterable interface. This loop has the advantage that there is no need to maintain a variable that acts as a counter, nor does it require establishing a condition to check if the end of the iteration has been reached, this avoids the possibility of creating an infinite loop. With the foreach loop a Collection is traversed as follows.
|
|
Loop with Iterable
But the forearch is for collections if you want to make a loop of a fixed number of iterations as in the first case, from 0 to 5, knowing that to use the foreach it is enough that we indicate an object that implements the interface Iterable we can use the following expression and its implementation that has the advantage of not having to include the initial value of the counter, the condition expression and the increment or decrement of the variable. The Counter class implements the Iterable interface and returns an Iterator on the values of the indicated range.
|
|
|
|
Loop with streams
In Java 8 with the introduction of Stream and IntStream we can use the range and rangeClosed to get a Stream of integers and make a loop with a behavior similar to the previous ones.
|
|
The Java 8 Stream are fine to simplify some complex operations but for a simple for loop it has its drawbacks such as significantly obfuscating the stacktrace in case of an exception. Any option can be used, but the first with the traditional for loop is the least recommended, having at our disposal the Counter class with Java 5 or the Stream and lambdas with Java 8.
Example with the different types of loop
The following program shows the four options, its output on the console would be the following:
|
|
For any of the ways of doing the for loop the behavior is the same, iterate a finite number of times or over the elements of a collection. Choosing which one to use among the different types of loops depends on the case and personal preferences but also considering the readability and expressiveness of the source code.
|
|
You can download the example complete source code from the Blog Bitix examples repository hosted on GitHub and test it on your computer by executing the following command:./gradlew run