For Loops

Apex support three types of For loops: traditional, list/set iteration, and SOQL.

The traditional For loop uses the following syntax:

for (init_statement; exit_condition; increment_statement) {
 code_block;
 }

The init_statement executes when the loop is first entered and can initialize more than one variable. The exit_condition is evaluated every time the loop executes. If the condition evaluates to false, the loop ends. If the condition evaluates to true, the code_block executes, followed by the increment statement.

A typical For loop of this variety is as follows:

for (Integer I = 0; I < 10 ; I++) {
 System.debug('Iteration ' + I);
 }

This loop prints a list to the debug console consisting of the word Iteration followed by the values ...

Get The Developer’s Guide to the Force.com Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.