An example of do-while loop.

// do-while loop checks the condition at the end of the loop
// The body of the loop gets executed at least once, even if
// the condition is false to begin with
// The loop is often used for data input (the stopping condition is 
// the end of the file which can only be detected when the program 
// attempts to read the next data item)

public class DoWhile {
    public static void main(String [] args) {
	int x = 1;
	do {
	    System.out.println("x = " + x);
	    x = x - 1;
	} while (x > 10); // the condition is after the body

	System.out.println("After the loop x = " + x);

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.