The traditional for loop in Apex corresponds to
the traditional syntax used in Java and other languages. In this syntax,
init_stmt, exit_condition,
increment_stmt, and code_block are placeholder
values. The syntax
is:
for (init_stmt; exit_condition; increment_stmt) {
code_block
}
When executing this type of for loop, the Apex
runtime engine performs the following steps, in order:
- Execute the init_stmt component of the loop. Note that multiple variables can be declared and/or initialized in this statement, separated by commas.
- Perform the exit_condition check. If true, the loop continues. If false, the loop exits.
- Execute the code_block.
- Execute the increment_stmt statement.
- Return to Step 2.
As an example, the following code outputs the numbers 1–10 into the debug log. Note that
an additional initialization variable, j, is
included to demonstrate the syntax:
for (Integer i = 0, j = 0; i < 10; i++) { System.debug(i+1); }