CSP and HTML Tag Reference
<CSP:LOOP>
[Home] [Back] [Next]
InterSystems: The power behind what matters   
Class Reference   
Search:    

Delimits the body of a FOR loop.
Synopsis
<CSP:LOOP>...</CSP:LOOP>
Attributes
General Attributes
Attribute Description Value
COUNTER Required. The name of the counter variable used by this loop. A valid variable name.
FROM The starting value of the loop counter. A numeric value or expression.
STEP The amount the loop counter is changed by on each iteration. A numeric value or expression.
TO The ending value of the loop counter. A numeric value or expression.
Description
The CSP:LOOP tag repeatedly executes its contents until a counter reaches the specified stop value.
The basic structure of a CSP FOR loop is:
<CSP:LOOP attributes>
content
</CSP:LOOP>
where attributes are the attributes of the CSP:LOOP tag (listed above) and content is the content to display during each iteration of the loop.
Counter in a FOR Loop
FOR loop execution is controlled by a server-side counter that is incremented during each iteration of the loop. The local variable used as a counter is specified by the COUNTER attribute:
<CSP:LOOP COUNTER="count">
where count is the name of the counter variable.
You must specify a COUNT attribute for every CSP:LOOP tag.
Initial Value of the Counter
You can specify the initial value of the counter using the FROM attribute:
<CSP:LOOP COUNTER="count" FROM="initval">
where count is the name of the counter variable and initval is its initial value.
If you do not specify a FROM attribute then the initial counter value will be 1.
Final Value of the Counter
You can specify the final value of the counter using the TO attribute:
<CSP:LOOP COUNTER="count" FROM="initval" TO="finalval">
where count is the name of the counter variable, initval is its initial value, and finalval is the last value that initiates a new iteration of the loop.
If you do not specify a TO attribute then the final counter value will be 1.
Counter Increment
You can specify the increment value of the counter using the STEP attribute:
<CSP:LOOP COUNTER="count" FROM="initval" TO="finalval" STEP="incr">
where count is the name of the counter variable, initval is its initial value, finalval is the last value that initiates a new iteration of the loop, and incr is the value added to the counter at the end of each iteration. incr does not have to evaluate to an integer; you can step by any number.
If you do not specify a STEP attribute then the increment value will be 1.
Take Value from Server
If you want to take values from the server, you can use:
<CSP:LOOP COUNTER="count" FROM="#(initval)#" TO="#(finalval)#" STEP="incr"> 
<CSP:LOOP> Examples
This simple FOR loop displays the value of the counter during each iteration of the FOR loop:
<UL>
<CSP:LOOP COUNTER="i" FROM="0" TO="6" STEP="1.5">
  <LI>The value of the counter is #(i)#.
</CSP:LOOP>
</UL>
This loop displays exam scores for a student, taking its end value from the server:
<TABLE>
<TR><TH>Exam</TH><TH>Score</TH></TR>
<CSP:LOOP COUNTER="i" FROM="1" TO="#(student.ExamScore.Count())#">
  <TR><TD>#(i)#</TD><TD>#(student.ExamScore.GetAt(i))#</TD></TR>
</CSP:LOOP>
</TABLE>
Specifying Script Language
The CSP:LOOP tag will fire one of two rules depending on whether the language is set to cache or basic; these two rules are %LOOP and %LOOPBASIC.