The Java do-while loop is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop.
The Java do-while loop is executed at least once because condition is checked after loop body.
do{ //code to be executed }while(condition);
public class DoWhileExample { public static void main(String[] args) { int i=1; do{ System.out.println(i); i++; }while(i<=10); } }
If you pass true in the do-while loop, it will be infinitive do-while loop.
do{ //code to be executed }while(true);
public class DoWhileExample2 { public static void main(String[] args) { do{ System.out.println("infinitive do while loop"); }while(true); } }
Now, you need to press ctrl+c to exit from the program.