The While Construct

The While construct is a simple statement which decides whether or not a piece of code should be executed.

It looks like this....

while condition

The code contained inside the while statement is executed while the condition is true.

If the condition is true then the code is executed. The condition is then tested again and, if it is true, the code is executed. This process continues until the condition eventually becomes false.

Note: If the condition is false to begin with, then the code is never executed.

This will loop forever (infinite loop) if the body of the while loop does not change the condition to evaluate to false.

Table of contents