Skip to content

MvFOR#


Loops through the code between and the number of time specified between first and last, incrimenting the index. Optionally you can exit the loop using .

Syntax
<MvFOR INDEX = "{ expression } | variable name" 
       COUNT = "{ expression }">
       FIRST = "{ expression }" 
       NEXT = "{ expression }">
       LAST = "{ expression }" 
       EXPR = "{ expression }">

       <MvFORSTOP>
       <MvFORCONTINUE> 
</MvFOR>

Loops through the code between <MvFOR> and </MvFOR> the number of times specified by the attributes, incrementing the index. Optionally 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 simple loops or execute more complex iterations.


Attributes#

Attribute Description
INDEX (Optional) Name of the variable used to track loop iterations. When specified, this variable is available inside the loop and in the EXPR, NEXT, and LAST expressions. If omitted, an internal variable is used.
FIRST (Optional) Numeric expression defining the starting value of the index. Evaluated once at the beginning of the loop.
NEXT (Optional) Expression evaluated at the end of each iteration to increment the index variable. Defaults to index + 1 if omitted.
LAST Expression defining the ending value (inclusive). Evaluated at loop start and before each iteration, allowing dynamic bounds. No performance penalty for static values.
COUNT Shorthand for a simple loop: iterates from 1 to COUNT, incrementing by 1. Evaluated once at loop start.
EXPR Boolean expression evaluated at the beginning of each iteration. The loop continues while true, otherwise it terminates.

Note

Exactly one of COUNT, LAST, or EXPR must be specified. All other attributes are optional. When using NEXT, ensure the expression increments the index.


Examples#

In its simplest form only COUNT is specified. If INDEX is provided, the loop variable becomes available inside the loop.

Basic COUNT Loop
<MvFOR COUNT="{ 100 }">
</MvFOR>

<MvFOR INDEX="l.pos" COUNT="{ 100 }">
  <MvEVAL EXPR="{ l.pos }"><br>
</MvFOR>

This loop runs from 1 to l.high, using FIRST, NEXT, and LAST, and demonstrates <MvFORCONTINUE> and <MvFORSTOP>.

FIRST/NEXT/LAST with FORCONTINUE and FORSTOP
<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>

Steps backwards by 10 from 100 to 10, using a math expression in NEXT for reverse iteration.

Reverse Loop by 10s
<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 behaves like <MvWHILE>. The loop continues while the expression is true.

Conditional Loop with EXPR
<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>