1) It defines a block of code to be executed as long as a particular condition is met. The condition is tested at the end of each iteration. It always guaranteed at least one pass through a do-while loop.
} while (condition);
where condition represents a binary expression that, if true, permits another iteration of the loop to be performed. If no longer true, processing continues at the next statement.
The braces may be omitted if the loop consists of a single statement. This would constitute a “single statement do-while loop”.
It can be used in place of nearly any while loop as shown by these examples:
Counting to 10 without a local variable
int i = 1;
} while (i <= 10);
Loop control variable i is initialized outside the loop. Within the loop, the current value of i is displayed and i is incremented. At the end of each iteration, the value of i is tested to determine if it is still less than or equal to 10. If so, the body of the loop is repeated. Otherwise processing will jump to the next statement.
An endless loop
System.out.println(“I won’t end”);
A small program using nested do-while loops to generate a 9 x 9 multiplication table
public class Aman
public static void main(String args)
// Initialize the row number.
int row = 1;
// This outer loop generates one row of the multiplication
// table during each iteration.
// Initialize the column number.
int column = 1;
// This inner loop generates one column of the current row
// of the multiplication table during each iteration.
// If a one digit number is about to be displayed, preceed it
// with four spaces. Otherwise, preceed it with three spaces.
if ((row * column) < 10)
// Display the number.
System.out.print((row * column));
// Increment the column number.
} while (column <= 9);
// End the current line.
// Increment the row number.
while (row <= 9);