while Loop

Purpose:

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

Mechanics:

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

Example:

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

Usage:

  • <continuation condition> is a variable or expression that evaluates to a boolean.
  • The /* Java statements */ can be any legal Java statements. If more than one statement is to be repeated, then curly brackets should be used. Otherwise, the curly brackets are optional.

Restrictions:

<continuation condition> is tested before the body of the loop is executed. If <continuation condition> is true, the body is executed. When the loop body has been executed, the <continuation condition> is checked again. The loop continues to execute until the <continuation conditon> is false.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu