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:

  1. The start_expression is evaluated.
  2. The test_expression is evaluated.
  3. If the test_expression is false (0), execution ends.
  4. If the test_expression is true (non-zero), the body is executed.
  5. If a break statement is encountered in the body, the loop terminates.
  6. 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.