This command is usually used together with <Array> to assign values (by cyclically executing <VariableCommand>) to array variables in a single code line. This reduces the code amount.
<!--Method 1: Use frequency to control cycle command execution. The value range of the variable in indexFlag is [0, frequency – 1].-->
<CycleCommand frequency="" indexFlag="" cycleCondition="">
<VariableCommand name="" type="" index="" expression=""/>
</CycleCommand>
<!--Method 2: Use begin and end to control cycle command execution. The value range of the variable in indexFlag is [begin,end].-->
<CycleCommand begin="" end="" indexFlag="" cycleCondition="">
<VariableCommand name="" type="" index="" expression=""/>
</CycleCommand> The cycle range of a cycle command can be [0,frequency – 1] or [begin,end]. A new value is automatically assigned to the array index variable in indexFlag before each cycle and <VariableCommand> is executed to assign values to variables in the array.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
indexFlag | String | Yes | Identifier of an index variable. The variable indicates the number of cycles and is referenced by expression in <VariableCommand>. |
frequency | Number | Yes | Number of cycles. The value range of the variable in indexFlag is [0,frequency – 1]. If this parameter is set, the settings of begin and end are invalid. |
begin | Number | No | Cycle beginning. The value range of the variable in indexFlag is [begin,end]. |
end | Number | No | Cycle end. The value range of the variable in indexFlag is [begin,end]. |
cycleCondition | Number | No | Cycle condition. When this parameter is not set to 0, <VariableCommand> is executed. The value can be a numeric expression. |
Example 1: Use <CycleCommand> together with <Array> to implement formatted display of integers from 0 to 99. The <Var> tag defines an array variable #find[] and the array contains 100 values. The <Array> tag assigns values to attributes such as textExp and coordinates of 100 texts. When a user taps the display button, <CycleCommand> is executed for 100 times and the value of the index variable #col in indexFlag increases from 0 to 99. In each cycle, <VariableCommand> assigns the value of #col in expression to #find[#col] (that is, to find element #col + 1 in the find array) and displays the elements in the array on the screen through the <Text> tag in <Array>. Note that type in <VariableCommand> and <Var> is set to number[], indicating that the find array is an integer array.
<Var name="find" type="number[]" size="100"/>
<Array x="100" y="300" frequency="100" indexFlag="__i" >
<Text textExp="#find[#__i]" size="30" align="center" x="#__i%10*90" y="int(#__i/10)*90"/>
</Array>
<Button x="0" y="0" w="1080" h="1920">
<Trigger action="up">
<CycleCommand frequency="100" indexFlag="col">
<VariableCommand name="find" type="number[]" index="#col" expression="#col"/>
</CycleCommand>
</Trigger>
</Button> Example 2: Use the begin and end parameters to assign values to elements in the find array (from find[0] to find[99]). This is another method to implement the cycle.
<CycleCommand begin="0" end="99" indexFlag="col">
<VariableCommand name="find" type="number[]" index="#col" expression="#col"/>
</CycleCommand> Example 3: Set cycleCondition to display integers from 0 to 99 except 20. Set cycleCondition to ne(#col, 20). When #col is not 20, ne(#col, 20) is set to 1, indicating that the variable assignment command is executed. When #col is 20, ne(#col, 20) is set to 0, indicating that the variable assignment command is not executed. In this case, the variable #find[20] has no value and therefore integers from 0 to 99 except 20 are displayed.
<Var name="find" type="number[]" size="100" />
<Array x="100" y="300" frequency="100" indexFlag="__i" >
<Text textExp="#find[#__i]" size="30" align="center" x="#__i%10*90" y="int(#__i/10)*90"/>
</Array>
<Button x="0" y="0" w="1080" h="1920">
<Trigger action="up">
<CycleCommand begin="0" end="100" indexFlag="col" cycleCondition="ne(#col,20)">
<VariableCommand name="find" type="number[]" index="#col" expression="#col"/>
</CycleCommand>
</Trigger>
</Button>