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.

1%%[
2  Var @repetitions
3  Set @repetitions = 5
4  For @counter = 1 to @repetitions do
5    OutputLine(Concat("<p>This is line "), v(@counter), "</p>")
6  Next @counter
7]%%

The code outputs this text:

1This is line 1
2This is line 2
3This is line 3
4This is line 4
5This 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.

1%%[
2  /* Initialize variables */
3  Var @dataString, @data, @rowCount, @row, @sku
4
5  /* Set @dataString to a pipe-delimited string of data */
6  Set @dataString = "938402993|858372392|648404923|293203492"
7
8  /* Build a rowset using the data in @dataString */
9  Set @data = BuildRowsetFromString(@dataString, "|")
10
11  /* Count the number of rows in the rowset */
12  Set @rowCount = RowCount(@data)
13
14  /* Break the AMPscript code block so that we can use some of these variables
15     in our HTML */
16]%%
17
18<!-- The inline AMPscript here outputs the value of the @rowCount variable -->
19<p>There are %%=v(@rowCount)=%% active SKUs:</p>
20
21<!-- Start a bulleted list -->
22<ul>
23
24%%[
25  /* Execute the commands in the For loop once for each row in the rowset */
26  For @counter = 1 to @rowCount do
27
28    /* Set the value of @row to the data in the current row (if @counter equals
29    1, set the value to the first row, and so forth) */
30    Set @row = Row(@data, @counter)
31
32    /* Set the value of @sku to the first item in the current row */
33    Set @sku = Field(@row, 1)
34
35  /* Break the AMPscript block again so that we can use the current value of
36     the @sku variable in the HTML */
37]%%
38
39<!-- Show the current value of @counter, the value of @rowCount, and the
40     current value of @sku -->
41<li>(%%=v(@counter)=%%/%%=v(@rowCount)=%%): %%=v(@sku)=%%</li>
42
43%%[
44    /* Iterate the counter */
45    Next @counter
46]%%
47
48<!-- Close the bulleted list -->
49</ul>

The output of this example contains this text:

1There are 4 active SKUs:
2
3  • (1/4): 938402993
4  • (2/4): 858372392
5  • (3/4): 648404923
6  • (4/4): 293203492