web analytics

Java do-while loop

Java do while loop executes block of code at least once i.e. it will not check the condition in first iteration. After first iteration it repeats the block of code until the condition remains true.

Syntax


do {
// block of code to be executed
}while (condition);

Example

Following is an example of do while loop. The code block will be executed at least once irrespective of condition is true or false be the code block is executed before testing of condition.


int index = 1;
do {
System.out.println("Value of index is :"+index);
index++;
}while (index < 10);

Important: Loop will never terminate if variable used in condition remains constant. Therefore it should be incremented / decremented according to situation.

One Reply to “Java do-while loop”

Comments are closed.