Website For Developers

Do while loop in Java


There are three types of loops in Java.


Do While Loop in Java

Syntax

do {
//Statements in the loop body
}
while (Boolean_expression/condition);


Example of do while loop in Java

public class DoWhileExample {
   public static void main(String[] args) {
     int value = 0;
     do {
       System.out.println( "Value is : " + value );
       value++;
     }
    while ( value < 5 )
   }
}

Output

Value is : 0
Value is : 1
Value is : 2
Value is : 3
Value is : 4
Value is : 5



Difference between while and do-while loop in Java