do-while loop is executed at least once irrespective of the condition. This loop executes all the statements present in the loop body before checking the condition.
Syntax
do { //code to be executed }while(true);
Example
public class Example1 { public static void main(String args[]) { int i=1; do { System.out.println(2*i); i++; }while(i<=10); } }
Output
2 4 6 8 10 12 14 16 18 20
Flowchart of do-while loop
By passing true in the do-while loop, you will get infinite do-while loop.
Syntax
do { //code to executed }while(true);
Example
public class Example { public static void main(String args[]) { do { System.out.println("This is infinite do while loop."); }while(true); } }
Output
This is infinite do while loop. This is infinite do while loop. This is infinite do while loop. This is infinite do while loop. ctrl+c
Press ctrl+c to exit infinite do-while loop.