C-style for Statement
Description
The C-style for statement is used to execute a loop. The body which follows the for statement can be either a single statement or set of statements inside braces ( { and } ). This statement executes as follows:
- The start_expression is evaluated.
- The test_expression is evaluated.
- If the test_expression is false (0), execution ends.
- If the test_expression is true (non-zero), the body is executed.
- If a break statement is encountered in the body, the loop terminates.
- The step_expression is evaluated.
Repeat steps 2 through 6 until the test_expression is false, or a break statement is encountered.
If the test_expression is false the first time it is tested, then the step expression and body are not executed.
Syntax
for (start_expression; test_expression; step_expression ) body
for (a=1; a <= 5; a+=1) print(a);
The statement prints the numbers from 1 to 5 until the test expression is false.
For more information, see the following: