---
schema_version: "1"
id: 40f0b1aef4402712b6c5a564e11ce75093e9c35410ce1289d3380d56e5c4ebc6
title: List or Set Iteration for Loops
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_loops_for_lists.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_lists.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 63dd81060b0ccc29a2eef31ebcb8089b10860526ed9eea177ca8c5888ec0a922
---
# List or Set Iteration for Loops

The list or set iteration for loop iterates over all the elements in a list or set. In this syntax, variable, list\_or\_set, and code\_block are placeholder values. The syntax is:

for (variable : list\_or\_set) {
    code\_block
}

where variable must be of the same primitive or sObject type as list\_or\_set.

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:

Integer\[\] myInts = new Integer\[\]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

for (Integer i : myInts) {
    System.debug(i);
}