Chapter 6: Controlling Program Flow with Loops 129 (Cedant web hosting)
Chapter 6: Controlling Program Flow with Loops 129 Figure 6-4: The action of the for loop in Listing 6-2. Set count to 1 Is count less than or equal to 10? yes The value of count is … Add 1 to count no Done Each of the three items in parentheses plays its own distinct role: The initialization is executed once, when the run of your program first reaches the forstatement. The expression is evaluated several times (before each iteration). The update is also evaluated several times (at the end of each iteration). If it helps, think of the loop as if its text is shifted all around: int count = 1 for count <= 10 { out.print( The value of count is ); out.print(count); out.println( . ); count++ } You can t write a real forstatement this way. Even so, this is the order in which the parts of the statement are executed. If you declare a variable in the initialization of a forloop, you can t use that variable outside the loop. For instance, in Listing 6-2, you get an error message if you try putting out.println(count)after the end of the loop. Anything that can be done with a forloop can also be done with a while loop. Choosing to use a forloop is a matter of style and convenience, not necessity.
With our Unix hosting accounts you have total control of your Web site content from anywhere in the world.For more information please follow link Unix Web Hosting.