Java


Java Tutorial


Admission Enquiry Form

  

Do While Loop in Java

What is do-while loop in Java?

do-while loop in Java is an Unknown loop in Java. It is used when the number of iteration are not known. It executes once before checking the condition.

Syntax of do-while loop.

do
{
//number of statements.
}while(condition);




Example of do-while loop:

// Java program of do-while loop.
public class DoWhileLoop {
public static void main(String[] args) {

int i;
i=1;
do
{
System.out.println("i is = "+i);
i++;

}while(i<=10); //end of do while loop

}//end of main method

}//end of class DoWhileLoop


Output:

i is = 1
i is = 2
i is = 3
i is = 4
i is = 5
i is = 6
i is = 7
i is = 8
i is = 9
i is = 10

In the above example, the do-while loop will execute 10 times, first time the statement in do block will be executed before checking the condition, another nine times statement will be executed after checking the condition in while.