next up previous
Next: Observations on the do Up: Unit 06 Previous: Example of for loop:

The do loop

In a while loop, the condition of end of loop is checked at the beginning of each iteration. A do loop is similar to a while loop, with the only difference that the condition of end of loop is checked at the end of each iteration.


do statement

Syntax:

do
  statement
while (condition);

Semantics: is equivalent to

statement;
while (condition)
  statement

Hence:

Example: Print out 100 stars.

int i = 0;
do {
  System.out.print("*");
  i++;
} while (i < 100);


next up previous
Next: Observations on the do Up: Unit 06 Previous: Example of for loop: