The while statement

The while statement loops while a condition is true. The while statement has the following syntax:

 

while (condition)
    statement   // keeps executing statement until 
condition is false

 

In the above example, the statement in the while block is executed as long as the condition is true. The condition is tested at the top of the loop each time. You can use {} brackets to group more than one statement. For example:

while (condition)
    {
    statement1
    statement2
    }