Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Do-while-loop

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
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.



This post first appeared on Java Programs, please read the originial post: here

Share the post

Do-while-loop

×

Subscribe to Java Programs

Get updates delivered right to your inbox!

Thank you for your subscription

×