Monday 16 December 2013

Do...while Loop example in java


A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Syntax:
The syntax of a do...while loop is:

do
{
   //Statements
//Increments or decremets

}while(Boolean_expression);

If the Boolean expression is true, the flow of control jumps back up to do, and the statements in the loop execute again. This process repeats until the Boolean expression is false.

Example:

public class Testfordo {

   public static void main(String args[]){
      int y = 10;

      do{
         System.out.print("value of y : " + y );
         y++;
         System.out.print("\n");
      }while( y < 30 );
   }
}

This would program produce the following result:

value of y : 20
value of y : 21
value of y : 22
value of y : 23
value of y : 24
value of y : 25
value of y : 26
value of y : 27
value of y : 28
value of y : 29

No comments:

Post a Comment

Comment