Java Reference
In-Depth Information
Output
The investment doubled after 15 years
A while statement is often called a loop. If you draw a flowchart, you will see that
the control loops backwards to the test after every iteration (see Figure 1 ).
The following loop,
while (true)
statement
executes the statement over and over, without terminating. Whoa! Why would you
want that? The program would never stop. There are two reasons. Some programs
indeed never stop; the software controlling an automated teller machine, a telephone
switch, or a microwave oven doesn't ever stop (at least not until the device is turned
off). Our programs aren't usually of that kind, but even if you can't terminate the loop,
you can exit from the method that contains it. This can be helpful when the
termination test naturally falls in the middle of the loop (see Advanced Topic 6.3 ).
Figure 1
Flowchart of a while Loop
231
Search WWH ::




Custom Search