while keyword
Syntax
>>-while-(-Expression-)-Statement-><
Description
The while statement executes an Expression and a Statement
repeatedly until the value of the Expression is false. If the Expression
is not of type boolean, a compilation error occurs.
A while statement is executed by first evaluating the Expression:
- If the value is true, then the contained Statement is
executed. After that, a decision is made, based on the following rules:
- If execution of the Statement completes normally, then the entire while
statement is executed again.
- If execution of the Statement completes abruptly, it is handled as described
below.
- If the value of the Expression is false, no further action
is taken and the while statement completes normally.
Abrupt completion of the contained Statement is handled in the following
manner:
- If execution of the Statement completes abruptly because of a break
with no label, no further action is taken and the while statement completes
normally.
- If execution of the Statement completes abruptly because of a continue
with no label, then the entire while statement is executed again.
- If execution of the Statement completes abruptly because of a continue
with label L, a decision is made, based on the following rules:
- If the while statement has label L, then the entire while
statement is executed again.
- If the while statement does not have label L, the while
statement completes abruptly because of a continue with label L.
- If execution of the Statement completes abruptly for any other reason, the while
statement completes abruptly for the same reason. Note that the case of abrupt completion
because of a break with a label is handled by the general rule for labeled
statements.
Example
The following is an example of a while statement:
int a = 1;
int b;
/*
* Execute the body of the loop until the value of
* a is greater than 25. The value of a + b is
* printed each time through the loop.
*/
while (a <=25) {
b = a * 10;
System.out.println(a + b);
a++;
}

Syntax diagrams
Labeled statements
break keyword
continue keyword
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.
