Chapter 6: Controlling Program Flow with Loops 131 (Paypal hosting)
Chapter 6: Controlling Program Flow with Loops 131 Listing 6-3 (continued) out.println( Ohhhhhhhh… ); out.println(); } out.print( Al s all wet. ); out.println( Oh, why is Al all wet? Oh, ); out.print( Al s all wet cause ); out.println( he s standing in the rain. ); } } Listing 6-3 is nice because it combines many of the ideas from Chapters 5 and 6. In Listing 6-3, two switchstatements are nested inside a forloop. One of the switchstatements uses breakstatements; the other switchstatement uses fall-through. As the value of the forloop s counter variable (verse) goes from 1to 2and then to 3, all the cases in the switchstatements are executed. When the program is near the end of its run and execution has dropped out of the forloop, the program s last four statements print the song s final verse. When I boldly declare that a forstatement is for counting, I m stretching the truth just a bit. Java s forstatement is very versatile. You can use a forstatement in situations that have nothing to do with counting. For instance, a statement with no update part, such as for (i = 0; i < 10; ), just keeps on going. The looping ends when some action inside the loop assigns a big number to the variable i. You can even create a forstatement with nothing inside the parentheses. The loop for ( ; ; )runs forever, which is good if the loop controls a serious piece of machinery. Usually, when you write a forstatement, you re counting how many times to repeat something. But, in truth, you can do just about any kind of repetition with a forstatement. Listing 6-3 uses breakstatements to jump out of a switch. But a break statement can also play a role inside a loop. To see an example, visit this book s Web site. Repeating Until You Get What You Want (Java do Statements) Fools rush in where angels fear to tread. Alexander Pope Today, I want to be young and foolish (or, at the very least, foolish). Look back at Figure 6-2 and notice how Java s whileloop works. As execution
The UK economy was the first in the world to enter the Industrial Revolution, and initially concentrated on heavy industry such as shipbuilding, coal mining, steel production and textiles.All our UK Web Hosting plans are very cheap and they fully support PHP mysql Dreamweaver frontpage and much more.