Skip to content

MvWHILE#


Loops through the code between and until the condition in EXPR is false. Optionally you can exit the loop using .

Syntax
<MvWHILE EXPR = "{ expression } | variable name">

         <MvWHILESTOP>
         <MvWHILECONTINUE> 
</MvWHILE>

Care must be taken to ensure an endless loop is not created. The Empresa engine will eventually time out, but the loop will consume server resources until that happens. The <MvWHILESTOP> command can be used to exit a loop at any time.

Attributes#

Attribute Description
EXPR Required, returns a value interpreted as a conditional expression, typically involving a comparison, string, or logic operator. The expression must return a true for Miva Script to process the code that follows the <MvWHILE> tag. An <MvWHILE> tag must have a closing </MvWHILE> end tag.

Examples#

Example 1
<MvWHILE EXPR = "{ condition }">
    Execute the code here repeatedly while this condition is true.
</MvWHILE>

In this example the code executes a fixed number of times.

Example 2
<MvASSIGN name="g.counter" value="1"> 
<MvWHILE EXPR = "{ Counter LE 10 }">
    Execute the code here 10 times.
    <MvASSIGN name="g.counter" value="{ g.counter + 1}"> 
</MvWHILE>

Breaking out of a loop. Loops can be ended manually using <MvWHILESTOP>, for example when an error is detected, or to prevent an endless loop.

Example 3
<MvWHILE EXPR = "{ 1 }">
    Execute the code here endlessly.

    <MvIF EXPR="{ g.counter GE 100 }">
        <MvWHILESTOP>
    </MvIF>
    <MvASSIGN name="g.counter" value="{ g.counter + 1}"> 
</MvWHILE>

Often when processing data, the number of loops is often not known in advance so other conditions must be tested. In this example, l.fieldlist is a comma separated list of values and is output to the browser one line at a time.

Example 4
<MvASSIGN NAME="l.posn" VALUE="{ 1 }">
<MvASSIGN NAME="l.item" VALUE="{ gettoken(l.fieldlist,',',l.posn) }">
<MvWHILE EXPR="{ l.item }">

    <MvEVAL EXPR="{ l.posn $ '. ' $ l.item }"><br>

    <MvASSIGN NAME="l.posn" VALUE="{ l.posn + 1 }">
    <MvASSIGN NAME="l.item" VALUE="{ gettoken(l.fieldlist,',',l.posn) }">
</MvWHILE>