Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
List or Set Iteration for Loops
The list or set iteration for loop iterates over
all the elements in a list or set. Its syntax
is:
where
variable must be of the same primitive or sObject type
as
list_or_set.
1for (variable : list_or_set) {
2 code_block
3}When executing this type of for loop, the Apex runtime engine assigns variable to each element in list_or_set, and runs the code_block for each value.
For example, the following code outputs the numbers 1 - 10 to the debug log:
1Integer[] myInts = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2
3for (Integer i : myInts) {
4 System.debug(i);
5}