RSS Feed

Java do Loop

A Java do loop is similar to the Java while loop, except that the while test happens at the bottom of the loop. This means that the loop always executes at least once.

Java do loop syntax

The syntax of a do loop in Java is:


do {
statement(s)
} while (expression);

Example Java do loop

Here is a Java do loop that prints the number 1 — even though the test fails.


int loopvar = 1; // Declare and initialize the loop counter
do {
System.out.println(loopvar); // Print the variable
loopvar = loopvar + 1; // Increment the loop counter
} while (loopvar >= 10); // Test and loop Java do Loop Java do Loop

Leave a Reply

Post your comments and questions below, but please follow our commenting guidelines.


Path: Home > Programming > Java > Java do Loop