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.


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


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);
}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.