404 users using DBSP now!
home -> documentation -> language reference -> FOR tag


Description

The main purpose of the FOR tag is to execute a block of code a given number of times.


Syntax

<#FOR [NAME="variablename"] STARTIN="anexpression" ENDIN="anexpression" [STEP="theStep"] [MAXITERATIONS="maxiterations"]>
.
.
<#/FOR>

NAME You can specify in varname a variable that contains the current value of the loop. This could be usefull when you need to use the loop index inside the block code.
STARTIN You can specify in anexpression an arithmetic expression where the result must be an INTEGER. This integer will serve as the initial value. If you want to know more about valid expressions, see the EXPRESSION property.
ENDIN You can specify in anexpression an arithmetic expression where the result must be an INTEGER. This integer will serve as final index. If you want to know more about valid expressions, see the EXPRESSION property.
STEP You can specify in thestep the increment to the loop index after each iteration.
MAXITERATIONS Specify in maxiterations the maximum number of cycles that the FOR could execute before breaking the loop. This property is used in order to avoid infinite loops on the server. If this property is not specified, the default value (6,500) is used.

References

WHILE
IF
DATASET


Applications

The FOR tag is one of the most important features of many languages, incluiding DBSP. This tag allows you to repeat HTML code fragments. FOR tags can be nested indefinitely within other FOR tags, which provides you with complete flexibility for unconditionally execution on any parts of your DBSP page.


Operative examples

Example 1. Single FOR.

Suppose that you wont count loops.

Source code


File: examples/FOR.dbsp

   <!--FOR.dbsp-->
  
 
    

FOR example 1.

 
     The loop starts in 2 and ends in 10

     <#SETCOOKIE NAME="COUNTER" VALUE="2"/>
     <#FOR STARTIN="2" ENDIN="10">
       <#COUNTER/> . - Loop

      <#SETCOOKIE NAME="COUNTER" EXPRESSION="COUNTER+1"/>  
     <#/FOR>
    <!-- Very important : Kill the variables not used anymore for saving resources -->
     <#KILLCOOKIE NAME="COUNTER"/>
  



Test example


Example 2. FOR with a custom step.

Suppose that you want set up a custom step for your loop. This example shows you how.

Source code


File: examples/forcookies.dbsp

   <!--FORCOOKIES.dbsp-->
  
 
    

FOR example 2.

 
   
     <#SETCOOKIE NAME="INITIALVALUE" VALUE="4"/>
     The loop starts in <#initialvalue/> and ends in <#finalvalue/> with an increment of 2

     <#FOR NAME="FORINDEX" STARTIN="INITIALVALUE" ENDIN="INITIALVALUE*8" STEP="2">
       <#FORINDEX/> . - Loop

     <#/FOR>
   
     <#KILLCOOKIE NAME="INITIALVALUE"/>
  
  


Test example