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