136 Part II: Writing Your Own Java Programs (Web hosting servers)
136 Part II: Writing Your Own Java Programs So in Listing 6-4, your hands are tied. The program s first real use of the replyvariable is inside the loop. But, to make that variable available after the loop, you have to declare replybefore the loop. In this situation, you re best off declaring the replyvariable without initializing the variable. Very interesting! To read more about variable initializations, see Chapter 4. To read more about blocks, see Chapter 5. Loops Made Painless I ll never forget the first time I played Clue. We bought the board game during a family vacation in Niagara Falls. (Niagara Falls?) Cousin Alan and I sat in the hotel room playing the game for hours on end. I scribbled complicated assertions in my little detective s notebook. At one point, I bluffed by suggesting three cards that I had in my hand. Alan took the bait and made an accusation that was wrong on all three counts. Because he d lost the game, and because I was being so smug about it, he beat the living daylights out of me. It was heaven. If I had the chance, I d do it all over again. The material in this section applies to Java 5, Java 6, or whatever higher version number comes along in the next few years. But this section s material doesn t work with older versions of Java versions such as 1.3, 1.4, and so on. For a bit more about Java s version numbers, see Chapter 2. Don t need no stinking counters The forloop in Listing 6-2 counts from 1 to 10. And its friend (the forloop in Listing 6-3) counts 1, 2, 3. This counting is very nice, but sometimes it s not the most natural way to think about a problem. Take, for instance, the listing of all possible accusations in the board game Clue. The suspects aren t numbered from 1 to 6, and neither are the rooms or weapons. You can number all these things, but why bother with numbering when the programming language provides a simpler solution? Java s enhanced for loop lets you cycle through groups of things without creating a counting variable. All you have to do is define the group. Listing 6-5 shows you what to do.
Do you want truly affordable web hosting? With us, what you see is what you get, just click on affordable web hosting services.