O'Reilly logo

The Developer’s Guide to the Force.com Platform by Phil Choi, Andrew Fawcett, Glen Martin, Rick Greenwald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required