---
schema_version: "1"
id: caa22508606bcae2beca423c0f19acd8f5c439c0febfacba2cacfff8cb736218
title: For Loops
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_loops_for.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 06bed738b78994583910c5ce2c2fcdc19723de0a1a8294bf7451f36c046b0667
---
# For Loops

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:
    
    for (variable : \[soql\_query\]) {
        code\_block
    }
    
    or
    
    for (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.

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

Curly braces ({}) are required around a code\_block only if the block contains more than one statement.

Each is discussed further in the sections that follow.

- **[Traditional For Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_traditional.htm)**
- **[List or Set Iteration for Loops](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_lists.htm)**
- **[Iterating Collections](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_iterating.htm)**

- [← Previous](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_while.htm "While Loops")