The while Statement

In this lesson, we will explore one of Java's three loop-control statements, the while statement.

Syntax

The while statement controls the execution of a while loop and has the following form:

while (condition)
    statement

Here, condition is a Boolean expression like those we encountered in the if statement. The while statement tests condition to see whether to continue the iteration. As long as condition is true, statement executes, as the following figure illustrates.

This statement is called the body of the loop. It can be—and usually is—a compound statement. It includes both the process step and the update step shown in this figure of the previous lesson. The initialization step—if one is ...

Access this course and 1400+ top-rated courses and projects.