do-while Loop

Purpose:

To repeat a block of code while a condition is true.

Mechanics:

do {
/* Java statements */
} while (<continuation condition>);

Example:

do {
/* Java statements */
} while (num < 10);

Usage:

  • <continuation condition> is a variable or expression that evaluates to a boolean.
  • The /* Java statements */ can be any number of legal Java statement. If more than one statement is used, then curly brackets must surround statements.

Restrictions:

  • The do-while structure tests the <continuation condition> after the loop body is performed, therefore the loop body is executed at least once.
  • When the <continuation condition> is false, the loop terminates and execution continues with the statement after the while clause.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu