while Statement
DescriptionThe 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:
- The test_expression is evaluated.
- If the test_expression is false (0), the loop terminates.
- If the test_expression is true (non-zero), the body executes.
- 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.
For more information, see the following: