{"id":31332,"date":"2018-04-21T15:47:45","date_gmt":"2018-04-21T15:47:45","guid":{"rendered":"https:\/\/ampscript.guide\/process-loops\/"},"modified":"2021-04-06T01:18:18","modified_gmt":"2021-04-06T01:18:18","slug":"process-loops","status":"publish","type":"post","link":"https:\/\/ampscript.guide\/process-loops\/","title":{"rendered":"Process Loops"},"content":{"rendered":"<h2>Process Loops<\/h2>\n<p>A process loop enables code or content within a block to be repeated until an ending index expression is reached. There are seven required parts in a process loop:<\/p>\n<ul>\n<li>an opening <code>for<\/code> statement<\/li>\n<li>a loop counter, defined as a variable<\/li>\n<li>a starting index expression<\/li>\n<li>a direction keyword; either <code>to<\/code> (to increase) or <code>downto<\/code> (to decrease)<\/li>\n<li>an ending index expression<\/li>\n<li>a <code>do<\/code> keyword<\/li>\n<li>a <code>next<\/code> keyword<\/li>\n<\/ul>\n<p>The opening <code>for<\/code> statement initializes the process loop. The scope of counter variable is different from other AMPscript variables (which have a local scope). This variable is set as a global scope and is locked from modification within the process loop.<\/p>\n<blockquote>\n<p>NOTE: The counter variable is locked from modification within a process loop. Any attempt to declare it will result in a validation or runtime error.<\/p>\n<\/blockquote>\n<p>The start and end index expressions must evaluate to an integer (not a decimal) and can use one of the following input types:<\/p>\n<ul>\n<li>a numeric constant<\/li>\n<li>an attribute or Data Extension value<\/li>\n<li>a variable<\/li>\n<li>a function<\/li>\n<\/ul>\n<p>The direction keyword determines whether the value of the variable increases or decreases by one with each iteration of the <code>for<\/code> loop. The variable value sets or increments by one after each iteration of the loop.<\/p>\n<p>The <code>next<\/code> statement closes the <code>for<\/code> loop. When the <code>next<\/code> keyword is reached, the system compares the end index expression to the value of the counter variable. If the value is not equal to the end index, the loop will repeat until the end index is reached.<\/p>\n<h3>Example 1<\/h3>\n<p>The following example sets the starting index as <code>1<\/code> and the ending index as <code>5<\/code>:<\/p>\n<div class=\"rcp_restricted rcp_paid_only\"><div>\n\n\n\t\n\t<form id=\"rcp_login_form\"  class=\"rcp_form\" method=\"POST\" action=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\">\n\n\t\t\n\t\t<fieldset class=\"rcp_login_data\">\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_login\">Username or Email<\/label>\n\t\t\t\t<input name=\"rcp_user_login\" id=\"rcp_user_login\" class=\"required\" type=\"text\"\/>\n\t\t\t<\/p>\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_pass\">Password<\/label>\n\t\t\t\t<input name=\"rcp_user_pass\" id=\"rcp_user_pass\" class=\"required\" type=\"password\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t\t<p>\n\t\t\t\t<input type=\"checkbox\" name=\"rcp_user_remember\" id=\"rcp_user_remember\" value=\"1\"\/>\n\t\t\t\t<label for=\"rcp_user_remember\">Remember me<\/label>\n\t\t\t<\/p>\n\t\t\t<p class=\"rcp_lost_password\"><a href=\"\/wp-json\/wp\/v2\/posts\/31332?rcp_action=lostpassword\">Lost your password?<\/a><\/p>\n\t\t\t<p>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_action\" value=\"login\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_redirect\" value=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_login_nonce\" value=\"339584e63b\"\/>\n\t\t\t\t<input id=\"rcp_login_submit\" class=\"rcp-button\" type=\"submit\" value=\"Login\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t<\/fieldset>\n\n\t\t\n\t<\/form>\n<br \/>\nNot a subscriber? <a href=\"\/subscriptions\/subscribe\/\">Subscribe now<\/a>.\n<\/div>\n<\/div>\n<h3>Example 2<\/h3>\n<p>Optionally, the <code>next<\/code> statement can be followed with the counter variable name:<\/p>\n<div class=\"rcp_restricted rcp_paid_only\"><div>\n\n\n\t\n\t<form id=\"rcp_login_form\"  class=\"rcp_form\" method=\"POST\" action=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\">\n\n\t\t\n\t\t<fieldset class=\"rcp_login_data\">\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_login\">Username or Email<\/label>\n\t\t\t\t<input name=\"rcp_user_login\" id=\"rcp_user_login\" class=\"required\" type=\"text\"\/>\n\t\t\t<\/p>\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_pass\">Password<\/label>\n\t\t\t\t<input name=\"rcp_user_pass\" id=\"rcp_user_pass\" class=\"required\" type=\"password\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t\t<p>\n\t\t\t\t<input type=\"checkbox\" name=\"rcp_user_remember\" id=\"rcp_user_remember\" value=\"1\"\/>\n\t\t\t\t<label for=\"rcp_user_remember\">Remember me<\/label>\n\t\t\t<\/p>\n\t\t\t<p class=\"rcp_lost_password\"><a href=\"\/wp-json\/wp\/v2\/posts\/31332?rcp_action=lostpassword\">Lost your password?<\/a><\/p>\n\t\t\t<p>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_action\" value=\"login\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_redirect\" value=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_login_nonce\" value=\"339584e63b\"\/>\n\t\t\t\t<input id=\"rcp_login_submit\" class=\"rcp-button\" type=\"submit\" value=\"Login\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t<\/fieldset>\n\n\t\t\n\t<\/form>\n<br \/>\nNot a subscriber? <a href=\"\/subscriptions\/subscribe\/\">Subscribe now<\/a>.\n<\/div>\n<\/div>\n<h3>Example 3<\/h3>\n<p>A common use case for AMPscript process loops is to look up multiple values from a Data Extension for a Contact or Subscriber; for example order line items to include in an order confirmation email.<\/p>\n<p>The following code retrieves &#8216;Name&#8217; and &#8216;Price&#8217; fields from a Data Extension for rows that match an order number attribute. The number of matching rows is counted using the <a href=\"\/rowcount\">RowCount<\/a> function and the field values are displayed as list items for the returned rows.<\/p>\n<div class=\"rcp_restricted rcp_paid_only\"><div>\n\n\n\t\n\t<form id=\"rcp_login_form\"  class=\"rcp_form\" method=\"POST\" action=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\">\n\n\t\t\n\t\t<fieldset class=\"rcp_login_data\">\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_login\">Username or Email<\/label>\n\t\t\t\t<input name=\"rcp_user_login\" id=\"rcp_user_login\" class=\"required\" type=\"text\"\/>\n\t\t\t<\/p>\n\t\t\t<p>\n\t\t\t\t<label for=\"rcp_user_pass\">Password<\/label>\n\t\t\t\t<input name=\"rcp_user_pass\" id=\"rcp_user_pass\" class=\"required\" type=\"password\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t\t<p>\n\t\t\t\t<input type=\"checkbox\" name=\"rcp_user_remember\" id=\"rcp_user_remember\" value=\"1\"\/>\n\t\t\t\t<label for=\"rcp_user_remember\">Remember me<\/label>\n\t\t\t<\/p>\n\t\t\t<p class=\"rcp_lost_password\"><a href=\"\/wp-json\/wp\/v2\/posts\/31332?rcp_action=lostpassword\">Lost your password?<\/a><\/p>\n\t\t\t<p>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_action\" value=\"login\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_redirect\" value=\"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/\"\/>\n\t\t\t\t<input type=\"hidden\" name=\"rcp_login_nonce\" value=\"339584e63b\"\/>\n\t\t\t\t<input id=\"rcp_login_submit\" class=\"rcp-button\" type=\"submit\" value=\"Login\"\/>\n\t\t\t<\/p>\n\t\t\t\t\t<\/fieldset>\n\n\t\t\n\t<\/form>\n<br \/>\nNot a subscriber? <a href=\"\/subscriptions\/subscribe\/\">Subscribe now<\/a>.\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Process Loops A process loop enables code or content within a block to be repeated until an ending index expression is reached. There are seven required parts in a process loop: an opening for statement a loop counter, defined as a variable a starting index expression a direction keyword; either to (to increase) or downto [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v14.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The AMPscript Guide - Process Loops<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ampscript.guide\/process-loops\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The AMPscript Guide - Process Loops\" \/>\n<meta property=\"og:description\" content=\"Process Loops A process loop enables code or content within a block to be repeated until an ending index expression is reached. There are seven required parts in a process loop: an opening for statement a loop counter, defined as a variable a starting index expression a direction keyword; either to (to increase) or downto [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ampscript.guide\/process-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"The AMPscript Guide\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-21T15:47:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-06T01:18:18+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ampscript.guide\/#website\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"The AMPscript Guide\",\"description\":\"The Definitive Scripting Manual for Salesforce Marketing Cloud\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/ampscript.guide\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/process-loops\/#webpage\",\"url\":\"https:\/\/ampscript.guide\/process-loops\/\",\"name\":\"The AMPscript Guide - Process Loops\",\"isPartOf\":{\"@id\":\"https:\/\/ampscript.guide\/#website\"},\"datePublished\":\"2018-04-21T15:47:45+00:00\",\"dateModified\":\"2021-04-06T01:18:18+00:00\",\"author\":{\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\"},\"breadcrumb\":{\"@id\":\"https:\/\/ampscript.guide\/process-loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ampscript.guide\/process-loops\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ampscript.guide\/process-loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/\",\"url\":\"https:\/\/ampscript.guide\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ampscript.guide\/process-loops\/\",\"url\":\"https:\/\/ampscript.guide\/process-loops\/\",\"name\":\"Process Loops\"}}]},{\"@type\":[\"Person\"],\"@id\":\"https:\/\/ampscript.guide\/#\/schema\/person\/5335042f77731e84f9808aecef25daec\",\"name\":\"dev\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","_links":{"self":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332"}],"collection":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/comments?post=31332"}],"version-history":[{"count":0,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/posts\/31332\/revisions"}],"wp:attachment":[{"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/media?parent=31332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/categories?post=31332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ampscript.guide\/wp-json\/wp\/v2\/tags?post=31332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}