361 users using DBSP now!
home -> documentation -> language reference -> WHILE tag


Description

The WHILE tag is used to repeat n times a block of HTML code based on a Boolean condition. This condition is evaluated and while the condition remains true the statement block of code is interpreted and its result inserted. Be careful when using this tag, because you could create infinite loops. However, the DBSP Engine will break the loop after bypassing the MAXITERATIONS value.


Syntax

<#WHILE EXPRESSION="anexpression" [MAXITERATIONS="maxiterations]">
.
.
<#/WHILE>

EXPRESSION Specify in anexpression a Boolean expression (the final result must be true or false; in other case, the DBSP Engine will raise an exception). If you want to know more about the kind of expressions you can use, see arithmetic expressions.
MAXITERATIONS Specify in maxiterations the maximum number of cycles that the while 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 (6500) is used.

References

FOR
IF
DATASET


Applications

The WHILE tag is one of the most important logic blocks in many programming languages, including DBSP. It allows you to conditionally insert code fragments. WHILE tags can be nested indefinitely within other WHILE tags, which provides you with complete flexibility for conditionally insertion and execution anywhere on your DBSP page.


Examples

Example 1. Simple WHILE.

Suppose that you have two variables, one bigger than the other. The first one increments its value until both values are equal. When this happens, the condition is satisfied and loop terminates.

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. While with random value.

In this example, the WHILE tag will stop until a random value becomes the expected value.

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



Example 3. Safe web colors palette using DBSP®.

We could use the WHILE tag for constructing the safe web colors table.

Source code


File: examples/SafeColors.dbsp


Safe web colors palette




 

Safe web colors palette using DBSP®


  

Due to the variety of Web browsers and applications available today, it's a necessity to have a subset of colors that could be reproduced faithfully regardless which hardware is used. This subset of colors is named "Safe colors" and in internet applications they are called "Safe web colors".


 
  

This example shows how to produce this palette using DBSP. Each channel of color (RGB) is limited to take one of these values: (00, 33, 66, 99, CC, FF). The table below is the result of the 6^3 possible colors. Place the mouse over any item to see the corresponding HEX color value.


 
 
  


   Safe color palette
  


   <#SETCOOKIE NAME="RED"   VALUE="255"/>
   <#SETCOOKIE NAME="GREEN" VALUE="255"/>
   <#SETCOOKIE NAME="BLUE"  VALUE="255"/>
  
   <#SETCOOKIE NAME="Z" VALUE="0"/>
  
  
   <#WHILE EXPRESSION="Z<=5">
     
     <#SETCOOKIE NAME="Y" VALUE="0"/>
    
    <#SETCOOKIE NAME="GREEN" VALUE="255"/>
    <#SETCOOKIE NAME="BLUE" EXPRESSION="BLUE-51"/>
    <#SETCOOKIE NAME="Z" EXPRESSION="Z+1"/>
    <#IF EXPRESSION="Z=3">
     
     
    <#/IF>  
   <#/WHILE>
  
  

   
     <#WHILE EXPRESSION="Y<=5">
 
          
        <#SETCOOKIE NAME="X" VALUE="0"/>
         <#WHILE EXPRESSION="X<=5">
          <#SETCOOKIE NAME="COLOR" EXPRESSION="'#'+HEX(BLUE,2)+HEX(GREEN,2)+HEX(RED,2)"/>
          
          <#SETCOOKIE NAME="RED" EXPRESSION="RED-51"/>
          <#SETCOOKIE NAME="X" EXPRESSION="X+1"/>
        <#/WHILE>
      
        <#SETCOOKIE NAME="RED" VALUE="255"/>
       <#SETCOOKIE NAME="GREEN" EXPRESSION="GREEN-51"/>
        <#SETCOOKIE NAME="Y" EXPRESSION="Y+1"/>
     <#/WHILE>
    

              
        

  


   Safe gray scale palette
  


   
 
   <#SETCOOKIE NAME="RED"   VALUE="0"/>
   <#SETCOOKIE NAME="GREEN" VALUE="0"/>
   <#SETCOOKIE NAME="BLUE"  VALUE="0"/>
  
     
          
        <#SETCOOKIE NAME="X" VALUE="0"/>
         <#WHILE EXPRESSION="X<=15">
          <#SETCOOKIE NAME="COLOR" EXPRESSION="'#'+HEX(BLUE,2)+HEX(GREEN,2)+HEX(RED,2)"/>
          
          <#SETCOOKIE NAME="RED" EXPRESSION="RED+17"/>
          <#SETCOOKIE NAME="GREEN" EXPRESSION="GREEN+17"/>
          <#SETCOOKIE NAME="BLUE" EXPRESSION="BLUE+17"/>
          <#SETCOOKIE NAME="X" EXPRESSION="X+1"/>
        <#/WHILE>
      
    

              
        

 



Test example