Alternate for loop syntax

September 9th, 2008

Java 5 introduced a number of syntax improvements, or at very least alternatives, to the language. One of these is a shortened version of the typical for loop construct.

A typical for loop looks like the following:

for (int ii = 0; ii < length; ii++) {
     String element = myArray[ii];
}

There are three components, separated by semi-colons. Any or all of these are optional (more on this later).

  • int ii = 0 – Variable declaration. Technically, many variables could be declared in this component. The important aspect is that the scope of variable is only the loop. Once the loop ends, any variables declared in this component disappear from scope.
  • ii < length – Condition. This must evaluate to a boolean that indicates whether or not to process another iteration of the loop. In that light, simply specifying true in this section will result in an infinite loop (unless break is called somewhere in the body of the loop).
  • ii++ – Post loop execution. Code in this section is evaluated at the end of the loop. This is typically used as an incrementing means to tie into the number of times the loop has executed. Also keep in mind this doesn’t have to simply increment the variable by one each time.

I mentioned that any or all of these are optional. It may seem obvious that the first and last components are optional; omitting them basically imitates the behavior of a while loop. But what happens if the center component is omitted? It is treated as always evaluating to true, thus resulting in an infinite loop. There are times, however rare, that an infinite loop is desired (either by expecting a break statement to execute in the loop or if it should run for the duration of the application), in which case the following can be used:

1
2
3
for (;;) {
    // Code
}

As I mentioned at the outset of this post, Java 5 introduced an alternate syntax. The syntax looks as follows:

1
2
3
for (String element : myArray) {
    // Code
}

The above code assumes myArray is an array or collection of String variables. At each iteration of the loop, the next String in the list is read and stored in the variable element, which, like the other for loop, is scoped only to code within the loop.

Through this syntax the explicit index counting is lost. In other words, unless I take extra steps to keep and increment a counter, at any time I cannot know how many times the loop has iterated. However, there are many cases where this knowledge is not needed, in which case this syntax is often quicker to both read and write.

One small tip I picked up from a former coworker. It may look odd that I chose ii as the name of the looping variable. The rationale is that I can easily search for usages of “ii” to see where the loop variable is used, whereas a single i will appear in many other places within the body of the loop. This is largely negated with the use of a good IDE, but is a simple convention I’ve adopted to provide this clarity in all cases.

Comments are closed.