Apex supports three variations of the
for loop:
- The traditional for loop:
for (init_stmt; exit_condition; increment_stmt) {
code_block
}
-
The list or set iteration
for loop:
for (variable : list_or_set) {
code_block
}
where
variable must be of the same primitive or sObject type as
list_or_set.
- The SOQL for loop:
for (variable : [soql_query]) {
code_block
}
orfor (variable_list : [soql_query]) {
code_block
}
Both variable and variable_list must be of the same sObject type as is returned by the soql_query.
Each is discussed further in the sections that follow.