Do while statement

The do while statement is similar to the while statement except that the condition test is performed at the bottom of the loop, and as a result the loop body will execute at least once. The syntax of the do while loop is the do keyword, a code block containing the body of the loop, and the while keyword followed by the conditional expression. The loop will continue to repeat as long as the conditional expression evaluates to true.

list = new LinkedList();
list.add(“A”);
list.add(”B”);
list.add(“C”);
do {
   System.out.println(list.removeFIrst());
} while(list.size() > 0);

In this example the loop continues while there are still elements that can be removed from the list. Since the do while loop executes at least once, they fail to properly handle situations where the loop conditions are not valid the first time through. If the above example started with an empty linked list then an error would have happened with the first call to list.removeFirst(). In many cases a while loop is a safer option because it can test for pre-conditions.