Apex - Buckles
Apex programming tutorial
Apex - Loops
Loops are used when a particular piece of code is to be repeated with the desired number of iterations. Apex supports the standard traditional for loop as well as other advanced types of loops. In this chapter, we will discuss loops in Apex in detail.
A loop instruction allows us to execute an instruction or a group of instructions multiple times and the following is the general of a loop instruction in most programming languages -
The following tables list the different loops that handle loopback requirements in the Apex programming language. Click on the following links to check their details.
| Sr.No. || Type and deion of the loop |
| 1 || for the loop |
This loop executes a set of instructions for each element of a recordset.
| SOQL for loop |
Executes a sequence of statements directly on the returned set of the SOQL query.
| 3 || Java-type for loop |
Execute a sequence of instructions in traditional Java similar syntax.
| 4 || while loop |
Repeat a statement or a group of statements as long as 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.