Apex Loops

Apex supports the three standard types of loops for implementing repeated actions: Do-while, While, and For loops.

Do-while Loops

A Do-while loop repeatedly executes a set of code statements. The syntax for the loop is as follows:

do {
 code_block;
       }
       while (Boolean_condition);

The code within the curly braces executes and then the condition is evaluated. A Do-while loop always executes at least once.

While Loops

A While loop is like a do-while loop, except that the while condition is evaluated before any of the statements in the code are executed. Because of this, the code in a While loop might not be executed at all. The syntax for the While loop is as follows:

while (Boolean_condition) {
 code_block;
       };

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.