|
Loops through the code between
Loops through the code between <MvFOR> and </MvFOR> the number of time specified between first and last, incrementing the index. Optionally you can exit the loop using <MvFORSTOP>. Loops execute a block of code a specified number of times, or while a specified condition is true. <MvFOR> can create the simplest of loops or execute more complex loop. Attributes
* Exactly one of COUNT, LAST, or EXPR must be specified. All other attributes are optional. When NEXT is used the expression must increment the INDEX ExamplesIn it simplest form only the COUNT is specified. If the INDEX is specified the variable becomes available within the loop. Example:<MvFOR COUNT = "{ 100 }"> </MvFOR> <MvFOR INDEX = "l.pos" COUNT = "{ 100 }"> <MvEVAL EXPR = "{ l.pos }"><br> </MvFOR> This number of iterations this loop performs is based on a variable in the LAST attribute. It loops from 1 to the l.high value incrementing the variable l.pos by 1 on each pass. In addition it illustrated the <MvFORCONTINUE> and <MvFORSTOP> tags. Example:<MvASSIGN NAME="l.high" VALUE="{ miva_array_max(l.orders) }"> <MvFOR INDEX = "l.pos" FIRST = "{ 1 }" NEXT = "{ l.pos + 1 }" LAST = "{ l.high }"> <MvCOMMENT> Skip the code every third loop </MvCOMMENT> <MvIF EXPR="{ l.pos MOD 3 }"> <MvFORCONTINUE> </MvIF> <MvCOMMENT> If this field is empty display an error and exit the loop. </MvCOMMENT> <MvIF EXPR="{ ISNULL l.orders[l.pos]:fname }"> <MvEVAL EXPR="{ 'Error in order number ' $ l.orders[l.pos]:id $ '. first name not found.' }"> <MvFORSTOP> </MvFOR> This example steps through the loop backwards, numbered 100 to 0, by a value of 10. Since the NEXT parameter must increment, a math calculation is used to count backwards. This could be used as a pointer to an array allowing you to iterate in reverse order. Example:<MvASSIGN NAME="l.max" VALUE="{ 100 }"> <MvFOR INDEX = "l.pos" FIRST = "{ 1 }" NEXT = "{ l.pos + 10 }" LAST = "{ l.max }"> <MvASSIGN NAME="l.ndx" VALUE="{ l.max + 1 - l.pos }"> <MvEVAL EXPR = "{ l.ndx }">, </MvFOR> Output: 100,90,80,70,60,50,40,30,20,10, The EXPR attribute is similar that found in the <MvWHILE> tag. The expression is evaluated at the beginning of each loop and while the expression evaluates as true (not zero), the loop continues. This examples processes 100 records, or as many as it finds. This could be used as an alternative to using <MvFORSTOP> to exit a loop. Example:<MvASSIGN NAME="l.found" VALUE="{ Get_First_Record(l.data) }"> <MvFOR INDEX = "l.pos" COUNT = "{ 100 }" EXPR = "{ l.found }"> ...process the record then get the next one... <MvASSIGN NAME="l.found" VALUE="{ Get_Record(l.pos, l.data) }"> </MvFOR> OR alternatively... <MvFOR INDEX = "l.pos" COUNT = "{ 100 }" EXPR = "{ Get_Record(l.pos, l.data) }"> ...process 100 records if found... </MvFOR>
|