while Statement

Description

The while statement is used to execute a loop. The body that follows the while statement can be a single statement or set of statements inside braces ( { and } ). This statement executes as follows:

  1. The test_expression is evaluated.
  2. If the test_expression is false (0), the loop terminates.
  3. If the test_expression is true (non-zero), the body executes.
  4. If a break statement is encountered in the body, the loop terminates.

Repeat steps 1 through 4 until the test_expression is false or a break statement is encountered.

If the test_expression is false the first time it is tested, the body is not executed.

Syntax

while (test_expression) body
a = 1;
while (a <= 10) {    
    print(a);    
a += 1;
}

The statement prints the numbers 1 through 10 while a <=10.