The while Statement
Explore how the while statement regulates repetition in Java through boolean conditions. Understand its syntax, counted loops for forward and backward counting, and increment/decrement operators. Learn to apply while loops for tasks such as counting and exponentiation, enhancing your ability to control repeated actions in your programs.
We'll cover the following...
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 needed—is performed separately before the while statement.
📝 ...