do-while Statement
Description
The C-style do-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 is executed as follows:
- The body is executed.
- If a break statement is encountered in the body, the loop terminates.
- The test expression is evaluated.
- If the test expression is false (0), the loop terminates.
- If the test expression is true (non-zero), steps 1 through 4 are repeated until a break statement is encountered or the test expression becomes false.
The body is always executed at least once.
Syntax
do body while (test_expression);
a = 1; do print(a++); while (a <= 10);
The statement prints the numbers 1 through 10.
For more information, see the following: