| home -> documentation -> environment guide -> Conditional loops: The WHILE tag |
The WHILE tag
The main purpose of the WHILE tag is to execute a block conditioned to the result of a Boolean expression. The general syntax of the while tag is described below:
<#WHILE EXPRESSIOn="anexpression">
...
<#/WHILE>
|
The WHILE tag repeats as long the expression given in the EXPRESSION property remains TRUE.
The expression is tested at the beginning of the loop, so if it is initially false, the loop code block will not be executed at all.
|
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. A simple WHILE
This example shows the general use of the WHILE Tag.
Source code
File: examples/while.dbsp
<!--WHILE.dbsp--> A Simple WHILE <#SETCOOKIE NAME="Start" VALUE="4"/> <#SETCOOKIE NAME="End" value="10"/> The while block will insert rows while Start<=End Start = <#START/> End = <#END/> Loop : <#WHILE EXPRESSION="START<=END"> Start = <#START/> <#SETCOOKIE NAME="START" EXPRESSION="START+1"/> <#/WHILE> <#KILLCOOKIE NAME="START"/> <#KILLCOOKIE NAME="END"/>
|
Test example
Example 2. A WHILE with a random condition.
In this example, the loop will continue while the condition holds.
Source code
File: examples/WhileRandom.dbsp
<!--WHILERANDOM.dbsp--> WHILE example 2. <#SETCOOKIE NAME="RANDOMVALUE" EXPRESSION="RANDOM(10)"/> In this example, the WHILE will stop until the random value becomes 7. <#WHILE EXPRESSION="RANDOMVALUE!=7"> RANDOMVALUE = <#RANDOMVALUE/> <#SETCOOKIE NAME="RANDOMVALUE" EXPRESSION="RANDOM(10)"/> <#/while> Finish value = <#RANDOMVALUE/> <#KILLCOOKIE NAME="RANDOMVALUE"/>
|
Test example
Previous | Next
|