Do-while-loop
-is similar to the while-loop
-statements inside a do-while loop are executed several times as long as the condition is satisfied -The main difference between a while and do-while loop:
The statements inside a do-while loop are executed at least once.
do-while loop has the form:
do
{
statement1;
statement2; . . .
}while( boolean_expression );
Example 1
int x = 0;
do {
System.out.println(x);
x++;
}while (x<10);
Example 2
//infinite loop
do{
System.out.println(“hello”);
} while (true);
Example 3
//one loop
// statement is executed once
do
System.out.println(“hello”);
while (false);
Coding Guidelines
Common programming mistakes when using the do-while loop is forgetting to write the semi-colon after the while expression.
do{ ... }
while(boolean_expression)//WRONG->forgot semicolon;
Just like in while loops, make sure that your do-while loops will terminate at some point.
Thursday, March 12, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment