Flow Control and Exception Handling - while Statement
Syntax (JLS §14.12)
do {
statement(s);
} while(boolean expression);
- executes statement(s) until expression is false
- statement(s) are always executed at least once
do {
System.out.println("Always executed at least once");
} while( false );
- break can be used to alter do-loop processing
do {
if( i==6 ) break; // exit loop
System.out.println(i);
i++;
}while( i < 10 );
- continue can be used to alter do-loop processing
do {
if( i==6 ) {
i--;
continue; // skip 6
} else {
System.out.println(i);
i--;
}
} while ( i >= 0);
Example Code
|