Apex - Loops



Loops are used when a particular piece of code should be repeated with the desired number of iteration. Apex supports the standard traditional for loop as well as other advanced types of Loops. In this chapter, we will discuss in detail about the Loops in Apex.

A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages −

Loop Architecture

The following tables lists down the different Loops that handle looping requirements in Apex Programming language. Click the following links to check their detail.

Sr.No. Loop Type & Description
1 for loop

This loop performs a set of statements for each item in a set of records.

2 SOQL for loop

Execute a sequence of statements directly over the returned set o SOQL query.

3 Java-like for loop

Execute a sequence of statements in traditional Java-like syntax.

4 while loop

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

5 do...while loop

Like a while statement, except that it tests the condition at the end of the loop body.

Advertisements