Java Reference
In-Depth Information
D.3.1 While
The while loop executes a block of statements as long as a given expression evaluates
to true . The expression is tested before execution of the loop body, so the body may be
executed zero times (i.e., not at all). This capability is an important feature of the while
loop.
while ( expression ) {
statements
}
Examples:
System.out.print( "Please enter a filename: " );
input = readInput();
while (input == null) {
System.out.print( "Please try again: " );
input = readInput();
}
int index = 0;
boolean found = false;
while (!found && index < list.size()) {
if (list.get(index).equals(item)) {
found = true ;
}
else {
index++;
}
}
D.3.2 do-while
The do-while loop executes a block of statements as long as a given expression evaluates to
true . The expression is tested after execution of the loop body, so the body always executes at
least once. This is an important difference from the while loop.
do {
statements
} while ( expression );
Example:
do {
System.out.print( "Please enter a filename: " );
input = readInput();
} while (input == null );
Search WWH ::




Custom Search