---
schema_version: "1"
id: c435c60350ee384b10ad6437063ed846b0b691bf11a4cc3f8f9d6c42bb05369d
title: For Loops
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/guide/language-basics/mc-ampscript-guide-language-basics-for-loops.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/guide/mc-ampscript-guide-language-basics-for-loops.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 69e00a04bb8b19efb8636664670e0f49312ec1af269585319ced60123e21fedf
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# For Loops

Use `For` loops to iterate through data structures or execute code a certain number of times.

At a minimum, a `For` statement must include:

- The keyword `For` immediately preceding the condition that you want to evalute.
- A variable to use as a counter, and its initial value.
- The keyword `to` or `downto` immediately following the value of the counter.
- The value at which the loop stops. This value can be a number, variable, or AMPscript function.
- The keyword `do`.
- The code to execute in each loop.
- The keyword `next` to iterate the counter. If you used the keyword `to`, the counter increases by one. If you used `downto`, the counter decreases by one.

These keywords aren’t case-sensitive; that is, `FOR`, `For`, and `for` all function in the same way.

## Usage

In their simplest form, `For` loops execute code a pre-determined number of times. This example shows how to output a string of text a specific number of times, printing the number of iteration in each line.

```
%%[
  Var @repetitions
  Set @repetitions = 5
  For @counter = 1 to @repetitions do
    OutputLine(Concat("<p>This is line "), v(@counter), "</p>")
  Next @counter
]%%
```

The code outputs this text:

```
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
```

## Iterating Through Data Structures

Several AMPscript functions can create or load data structures, such as data extensions. For example, when you provide it with a string of text, the `BuildRowsetFromString()` function creates a data structure called a "rowset," which consists of one or more lines of tabular data.

This example shows how to parse the data in this rowset. It also demonstrates how you can close and resume AMPscript code blocks to suit your desired output. The comments in the example explain the purpose of each line.

```
%%[
  /* Initialize variables */
  Var @dataString, @data, @rowCount, @row, @sku

  /* Set @dataString to a pipe-delimited string of data */
  Set @dataString = "938402993|858372392|648404923|293203492"

  /* Build a rowset using the data in @dataString */
  Set @data = BuildRowsetFromString(@dataString, "|")

  /* Count the number of rows in the rowset */
  Set @rowCount = RowCount(@data)

  /* Break the AMPscript code block so that we can use some of these variables
     in our HTML */
]%%

<!-- The inline AMPscript here outputs the value of the @rowCount variable -->
<p>There are %%=v(@rowCount)=%% active SKUs:</p>

<!-- Start a bulleted list -->
<ul>

%%[
  /* Execute the commands in the For loop once for each row in the rowset */
  For @counter = 1 to @rowCount do

    /* Set the value of @row to the data in the current row (if @counter equals
    1, set the value to the first row, and so forth) */
    Set @row = Row(@data, @counter)

    /* Set the value of @sku to the first item in the current row */
    Set @sku = Field(@row, 1)

  /* Break the AMPscript block again so that we can use the current value of
     the @sku variable in the HTML */
]%%

<!-- Show the current value of @counter, the value of @rowCount, and the
     current value of @sku -->
<li>(%%=v(@counter)=%%/%%=v(@rowCount)=%%): %%=v(@sku)=%%</li>

%%[
    /* Iterate the counter */
    Next @counter
]%%

<!-- Close the bulleted list -->
</ul>
```

The output of this example contains this text:

```
There are 4 active SKUs:

  • (1/4): 938402993
  • (2/4): 858372392
  • (3/4): 648404923
  • (4/4): 293203492
```
