I'm very comfortable with using for loops in AMPscript. However, someone recently mentioned that other loop contexts are supported, similar to JavaScript, which offers:

  • for - loops through a block of code a number of times
  • for/in - loops through the properties of an object
  • while - loops through a block of code while a specified condition is true
  • do/while - also loops through a block of code while a specified condition is true

I'm not sure which of the above is supported in AMPscript. I can't find any documentation or example code to indicate this extended support. Before I validate this for myself, has anyone tried this?

  • I have found no support for do/while or while (after a lot of digging). I can't say I have a definitive 'no', but I would say I am 99% certain. I just tried For/IN and got the syntax error that it 'requres TO or DOWNTO'. Commented Jul 18, 2017 at 20:03
  • You can use a SSJS try/catch to trap AMPScript errors. That might yield some extra details in the experiment. Commented Jul 18, 2017 at 20:49

4 Answers 4


I have put some time into this and in the resources it only lists FOR/TO(DOWNTO) as a viable looping option.

I have attempted FOR/IN and received a definitive syntax error that FOR requires to be followed by TO or DOWNTO - which means the functionality is definitely not there.

I then investigated using WHILE, which has no documentation at all and provided no errors when used - BUT it did not actually do what it was supposed to do. After looking through the code and using AMPScript editors to check syntax, etc. I have found out that both DO and WHILE are not recognized as commands. This means that there is no inherit capability to achieve these.

Basically what it comes down to is that the only available inherit looping function in AMPScript is FOR/TO(DOWNTO) and without tremendous 'hacking' of the inherit functionality (not even sure THAT is possible), of which I have not yet found a good use case, it is not possible to use the other loop types directly in AMPScript. You would need to use SSJS to achieve these.

SFMC support (helpful as always) directed me to the documentation resource listed above and told me those are the only available commands/functions.


My hacky stupid workaround for While loops in AMPscript:

%%[ FOR @i=1 to 2 DO
      /*Execute your code here*/
      IF NOT @condition THEN
        @i = Subtract(@i, 1)
    NEXT @i ]%%

This loop will keep executing until the condition is met.

  • 1
    Nice workaround! Commented Jul 20, 2017 at 20:22
  • Be careful- I have been told that the for loop variable is 'locked' and cannot be edited. I haven't tested it, but if this is the case, it can cause inconsistencies in your workaround. Commented Jul 20, 2017 at 23:18
  • 2
    @ncv - I attempted your code (honestly in hopes I was wrong, because that would help solve so much) but I get the following syntax error: The variable @i is in use by a processing loop and cannot be declared or set. Owning process: FOR LOOP 1. It appears this is not a viable workaround :( Commented Jul 21, 2017 at 12:23
  • 2
    You can set your own index as a workound to this workaround. just set an @index variable and increment that Commented Aug 30, 2018 at 18:42

Based on ncv solution:

        SET @x = 2
        FOR @i=1 to @x DO
           /*Execute your code here*/
           IF NOT @condition THEN
             @x = add(@x, 1)
        NEXT @i 

Short answer: Only (restricted) FOR loops are supported.

Salesforce's current official answer is documented at: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/languageElements.htm

As of January 2021 that text reads:

Using the FOR Process Loop

The FOR statement allows content within the FOR block to iterate over a variable number of times.

%%[FOR @Variable = <start expression> TO|DOWNTO <end expression> DO ]%%
        [wrapped script or email content]
    %%[NEXT @Variable]%%

The system locks the @Variable variable from modification within the process loop.

The start and end index expressions can use any one of the four types of input that evaluates to an integer:

Numeric constant Attribute or data extension value Variable Function call, such as LookupRows() The TO or DOWNTO keywords determine whether the value of the variable increases or decreases by one (1) with each iteration of the FOR loop. The variable value sets or increments by one (1) at the end of each iteration of the loop. The system compares the end index expression to the new value of the counter variable. If the value did not yet reach the end value or did not equal the end value, the loop will continue.

The NEXT statement closes the FOR loop. Optionally, follow the statement with the variable name controlling the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .