next up previous
Next: Complete set of control Up: Unit 06 Previous: Example of a do

Equivalence between while loop and do loop

As is clear from the semantics, each do loop can be replaced with an equivalent while loop. However, to do so, we need to duplicate the body of the do loop.

Example:

int i;

do {
  i = Integer.parseInt(JOptionPane.showInputDialog(
                         "Input a positive integer"));
} while (i <= 0);

equivale a

int i;

i = Integer.parseInt(JOptionPane.showInputDialog(
                       "Input a positive integer"));
while (i <= 0) {
  i = Integer.parseInt(JOptionPane.showInputDialog(
                         "Input a positive integer"));
}


next up previous
Next: Complete set of control Up: Unit 06 Previous: Example of a do