Loops
For Each Loop
For Each loops are able to iterate over a List variable. In this example, a list named
TestVar1
is looped over and each element is outputed to the console:
<dcs.Var Name="TestVar1" Type="List">
<AddItem Type="String" Value="car" />
<AddItem Type="String" Value="sun" />
<AddItem Type="String" Value="lime" />
</dcs.Var>
<dcs.ForEach Name="Item" In="$TestVar1">
<dc.Out>Got: {$Item}<br/></dc.Out>
</dcs.ForEach>
Output:
Got: car
Got: sun
Got: lime
We can also reference the index position of an item in the list with
_forindex
. See the slightly revised previous example below:
<dcs.Var Name="TestVar1" Type="List">
<AddItem Type="String" Value="car" />
<AddItem Type="String" Value="sun" />
<AddItem Type="String" Value="lime" />
</dcs.Var>
<dcs.ForEach Name="Item" In="$TestVar1">
<dc.Out>Got: {$Item} from pos: {$_forindex}<br/></dc.Out>
</dcs.ForEach>
Output:
Got: car from pos: 0
Got: sun from pos: 1
Got: lime from pos: 2
For Loop
This loop will iterate over an internal counter to produce a fixed number of cycles. In the example below, a counter is set at 5 initially and will run to 11.
<dcs.For Name="Counter" From="5" To="11">
<dc.Out>Got: {$Counter}<br/></dc.Out>
</dcs.For>
Output:
Got: 5
Got: 6
Got: 7
Got: 8
Got: 9
Got: 10
Got: 11
The default increment increase is 1, but this can be overriden with the
Step
parameter:
<dcs.For Name="Counter" From="5" To="11" Step="2">
<dc.Out>Got: {$Counter}<br/></dc.Out>
</dcs.For>
Output:
Got: 5
Got: 7
Got: 9
Got: 11
A negative
Step
can be set to iterate backwards if desired:
<dcs.For Name="Counter" From="12" To="3" Step="-3">
<dc.Out>Got: {$Counter}<br/></dc.Out>
</dcs.For>
Output:
Got: 12
Got: 9
Got: 6
Got: 3
UNFINISHED: unclear how
_forindex
functions within
For
loop, as there's no index position from a list to reference
While Loops
These loops will run while the
Target
remains true in the expression defined in the top line. In the below example,
TestVar1
is set to 4 initially, and the
While
loop runs as long as
TestVar1
is greater than 1.
<dcs.Var Name="TestVar1" Type="Integer" SetTo="4" />
<dcs.While Target="$TestVar1" GreaterThan="1">
<dc.Out>TestVar1 is {$TestVar1}<br/></dc.Out>
<dcs.With Target="$TestVar1">
<Dec />
</dcs.With>
</dcs.While>
Output:
TestVar1 is 4
TestVar1 is 3
TestVar1 is 2
Until Loops
This can be seen as an inverse to the While loop: the loop will continue to run until the expression defined in the first line returns true. In this example,
TestVar1
is set to 8 initially, and the
Until
loop runs until
TestVar1
is less than 5.
<dcs.Var Name="TestVar1" Type="Integer" SetTo="8" />
<dcs.Until Target="$TestVar1" LessThan="5">
<dc.Out>TestVar1 is {$TestVar1}<br/></dc.Out>
<dcs.With Target="$TestVar1">
<Dec />
</dcs.With>
</dcs.Until>
Output:
TestVar1 is 8
TestVar1 is 7
TestVar1 is 6
TestVar1 is 5
Continue Keyword
The
Continue
keyword will skip the current iteration of the loop. It is usually combined with an If conditional statement to detect appropriate logical conditions to skip.
In the example below, the numbers between 5 and 18 are outputted, skipping over 7 through 11.
<dcs.For Name="Counter" From="5" To="18">
<dcs.If Target="$Counter" GreaterThanOrEqual="7">
<dcs.If Target="$Counter" LessThanOrEqual="11">
<dcs.Continue />
</dcs.If>
</dcs.If>
<dc.Out>Got: {$Counter}<br/></dc.Out>
</dcs.For>
Output:
Got: 5
Got: 6
Got: 12
Got: 13
Got: 14
Got: 15
Got: 16
Got: 17
Got: 18
Break Keyword
The
Break
keyword will exit the entire loop and again is usually combined with an If statement to detect the proper condition.
The below example iterates over the numbers between 5 and 18 and outputs them, until it detects 12.
<dcs.For Name="Counter" From="5" To="18">
<dcs.If Target="$Counter" GreaterThanOrEqual="12">
<dcs.Break />
</dcs.If>
<dc.Out>Got: {$Counter}<br/></dc.Out>
</dcs.For>
Output:
Got: 5
Got: 6
Got: 7
Got: 8
Got: 9
Got: 10
Got: 11