by Dinesh Thakur

The do loop enables us to repeatedly execute a block of code until a boolean expression evaluates to false. It is almost identical to the while loop with the difference that here the logical expression is evaluated at the bottom of the loop rather than the top. This means that the contents of the loop will execute at least once.

 

Syntax:

do

{

    statement(s);

}  while (expression);

 

The keyword do begins the do construct. This is followed by a statement or a block. Next is the keyword while, followed by the parentheses containing an expression that must evaluate to a boolean value i.e. true or false.

When a do loop is encountered, the statement or block following the do keyword is executed. When the do loop body completes, logical expression is evaluated. If it is false, execution will continue with the next statement following the do loop. If it is true, the body of the do loop will be executed again. The body will continue to execute until the expression evaluates to false.

/* Print the sequence numbers from 1 to 10 using do while loop */

class DoWhile

{

            public static void main(String args[] )

            {

                        int i;

                        i=1;

                        do

                        {

                                    System.out.print(i+ " ");

                                    i++;

                        }while(i<=10);

            }

}