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 @repetitions3 Set @repetitions = 54 For @counter = 1 to @repetitions do5 OutputLine(Concat("<p>This is line "), v(@counter), "</p>")6 Next @counter7]%%
The code outputs this text:
1This is line 12This is line 23This is line 34This is line 45This 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, @sku45 /* Set @dataString to a pipe-delimited string of data */6 Set @dataString = "938402993|858372392|648404923|293203492"78 /* Build a rowset using the data in @dataString */9 Set @data = BuildRowsetFromString(@dataString, "|")1011 /* Count the number of rows in the rowset */12 Set @rowCount = RowCount(@data)1314 /* Break the AMPscript code block so that we can use some of these variables15 in our HTML */16]%%1718<!-- The inline AMPscript here outputs the value of the @rowCount variable -->19<p>There are %%=v(@rowCount)=%% active SKUs:</p>2021<!-- Start a bulleted list -->22<ul>2324%%[25 /* Execute the commands in the For loop once for each row in the rowset */26 For @counter = 1 to @rowCount do2728 /* Set the value of @row to the data in the current row (if @counter equals29 1, set the value to the first row, and so forth) */30 Set @row = Row(@data, @counter)3132 /* Set the value of @sku to the first item in the current row */33 Set @sku = Field(@row, 1)3435 /* Break the AMPscript block again so that we can use the current value of36 the @sku variable in the HTML */37]%%3839<!-- Show the current value of @counter, the value of @rowCount, and the40 current value of @sku -->41<li>(%%=v(@counter)=%%/%%=v(@rowCount)=%%): %%=v(@sku)=%%</li>4243%%[44 /* Iterate the counter */45 Next @counter46]%%4748<!-- Close the bulleted list -->49</ul>
The output of this example contains this text:
1There are 4 active SKUs:23 • (1/4): 9384029934 • (2/4): 8583723925 • (3/4): 6484049236 • (4/4): 293203492