Repetition statement—repeats an action while a condition remains true.
Pseudocode
While there are more items on my shopping list
Purchase next item and cross it off my list
The repetition statement’s body may be a single statement or a block.
Eventually, the condition will become false. At this point, the repetition terminates, and the first statement after the repetition statement executes.
Example of Java’s
while
repetition statement:
find the first power of 3 larger than 100.
Assume
int
variable
product
is initialized to
3.
while
( product <=
100
)
product =
3
* product;
Each iteration multiplies product by 3, so product takes on the values 9, 27, 81 and 243 successively.
When variable product becomes 243, the while-statement condition—product <= 100—becomes false. Repetition terminates. The final value of product is 243.
Program execution continues with the next statement after the while statement.