| home -> documentation -> environment guide -> Defined loops: The FOR tag |
A loop allows you to execute a statement or block of statements repeatedly. The need of repeat a block of code arises in almost every program.
The FOR Tag
The main purpose of the for-loop is to execute a block of code a given number of times. The general syntax of the FOR tag is described below:
<#FOR STARTIN="a" ENDIN="b">
…
<#/FOR>
|
In this case, the block will be inserted b-a+1 times, where a and b are integer values.
Another variation of the FOR tag is described below:
<#FOR NAME="varname" STARTIN="a" ENDIN="b">
…
<#/FOR>
|
In this case, the current value of the cycle will be assigned to a variable named as varname and you could use this value for your convenience.
|
Note: In order to avoid infinite loops that could cause a server crash, the maximum iterations that could be executed are by default 6,500. If the code iterates more times than this value, an exception is raised.
|
Examples
Example 1. FOR tag basis.
This example shows a very basic use of the FOR Tag.
Source code
File: examples/For.dbsp
<HTML> <!--FOR.dbsp--> <BODY> <H1>FOR example 1.</H1> The loop starts in 2 and ends in 10<BR> <#SETCOOKIE NAME="COUNTER" VALUE="2"/> <#FOR STARTIN="2" ENDIN="10"> <#COUNTER/> . - Loop<br> <#SETCOOKIE NAME="COUNTER" EXPRESSION="COUNTER+1"/> <#/FOR> <!-- Very important : Kill the variables not used anymore for saving resources --> <#KILLCOOKIE NAME="COUNTER"/> </body> </html>
|
Test example
Example 2. For with variation
Same as the above example, but using the FOR variation instead.
Source code
File: examples/Ex7_3_2.dbsp
<HTML> <!--FOR.dbsp--> <BODY> <H1>FOR variation</H1> <hr> The loop starts in 2 and ends in 10<BR> <#FOR NAME="COUNTER" STARTIN="2" ENDIN="10"> <#COUNTER/> . - Loop<br> <#/FOR> </body> </html>
|
Test example
Previous | Next
|