How to Create an Enhanced for Loop in Java - dummies

By Barry Burd

To understand how to use Java’s enhanced for statement, consider how the laws of probability work. Your chance of winning one of the popular U.S. lottery jackpots is roughly 1 in 135,000,000.

If you sell your quarter-million dollar house and use all the money to buy lottery tickets, your chance of winning is still only 1 in 540. If you play every day of the month (selling a house each day), your chance of winning the jackpot is still less than 1 in 15.

To illustrate the idea of the enhanced for statement, you will see four symbols — a cherry, a lemon, a kumquat, and a rutabaga.

When you play this simplified slot machine, you can spin any one of over 60 combinations — cherry+cherry+kumquat, rutabaga+rutabaga+rutabaga, or whatever. The goal here is to list all possible combinations. But first, let’s take a look at another kind of loop. This code defines an enum type for a slot machine’s symbols and displays a list of the symbols.

import static java.lang.System.out;
class ListSymbols {
 enum Symbol {
 cherry, lemon, kumquat, rutabaga
 }
 public static void main(String args[]) {
 for (Symbol leftReel : Symbol.values()) {
  out.println(leftReel);
 }
 }
}

This code uses Java’s enhanced for loop. The word “enhanced” means “en-hanced compared with the loops in earlier versions of Java.” The enhanced for loop was introduced in Java version 5.0. If you run Java version 1.4.2 (or something like that), you can’t use an enhanced for loop.

Here’s the format of the enhanced for loop:

for (TypeName variableName : RangeOfValues) {
Statements
}

Here’s how the loop follows the format:

  • The word Symbol is the name of a type.

    The int type describes values like –1, 0, 1, and 2. The boolean type describes the values true and false. And the Symbol type describes the values cherry, lemon, kumquat, and rutabaga.

  • The word leftReel is the name of a variable.

    The loop in Listing 15-1 defines count to be an int variable. Similarly, the loop in Listing 15-5 defines leftReel to be a Symbol variable. So in theory, the variable leftReel can take on any of the four Symbol values.

  • The expression Symbol.values() stands for the four values in the code.

    To quote myself in the previous bullet, “in theory, the variable leftReel can take on any of the four Symbol values.” Well, the RangeOfValues part of the for statement turns theory into practice. This third item inside the parentheses says, “Have as many loop iterations as there are Symbol values, and have the leftReel variable take on a different Symbol value during each of the loop’s iterations.”

    So the loop undergoes four iterations — an iteration in which leftReel has value cherry, another iteration in which leftReel has value lemon, a third iteration in which leftReel has value kumquat, and a fourth iteration in which leftReel has value rutabaga. During each iteration, the program prints the leftReel variable’s value.

    image0.jpg

In general, a someEnumTypeName.values() expression stands for the set of values that a particular enum type’s variable can have. For example, you can use the expression WhoWins.values() to refer to the home, visitor, and neither values.

The difference between a type’s name (like Symbol) and the type’s values (as in Symbol.values()) is really subtle. Fortunately, you don’t have to worry about the difference. As a beginning programmer, you can just use the .values() suffix in an enhanced loop’s RangeOfValues part.