Java Reference
In-Depth Information
10.4. while and dowhile
The while loop looks like this:
while ( expression )
statement
The expressionagain either of boolean or Boolean typeis evaluated and, if
it is TRue , the statement (which may be a block) is executed. Once the
statement completes, the expression is reevaluated and, if still TRue , the
statement is executed again. This repeats until the expression evaluates
to false, at which point control transfers to after the while .
We introduced the while loop with our second program in Chapter 1 , the
Fibonacci program:
while (hi < MAX) {
System.out.println(hi);
hi = lo + hi;
lo = hi - lo;
}
This loops around printing and calculating new Fibonacci values until the
highest value computed exceeds the maximum limit.
A while loop executes zero or more times since the expression might be
false the first time it is evaluated. Sometimes you want to execute a loop
body at least once, which is why you also have a dowhile loop:
do
statement
while ( expression );
 
Search WWH ::




Custom Search