Skip to content

While Steps

while (condition) {
  loop body
}
1. condition is checked. If true, execute the loop block. 2. loop body executes. 3. Go to step 1.

The condition in a while loop is always checked. * This means a while loop can execute 0 or more times. * That is, if the condition is false when Java first reaches the loop, then the loop never executes at all.

If the condition never becomes false, we end up in an "infinite loop". * This can be useful, but may be a bug.

Drill

WhileLoops/src/drills/WhileLoopSteps.java * Write a while loop that will accept a number from the user, multiply it by 2, and print the value to the screen. The program should accept data from the user until the user enters zero.


Prev -- Up -- Next