Loop structures: Difference between revisions

Content added Content deleted
Line 382: Line 382:
===For Loop===
===For Loop===
A <CODE>for</CODE> loop is really a <CODE>foreach</CODE> loop that can work with range operators or iterate through various data structures. The <CODE>to</CODE> operator creates an enumerating expression that lazily steps through its range.
A <CODE>for</CODE> loop is really a <CODE>foreach</CODE> loop that can work with range operators or iterate through various data structures. The <CODE>to</CODE> operator creates an enumerating expression that lazily steps through its range.
<lang frink>
<syntaxhighlight lang="frink">
for i = 1 to 1000000
for i = 1 to 1000000
{
{
println[i]
println[i]
}
}

</lang>
</syntaxhighlight>


The <CODE>to</CODE> operator can be combined with a <CODE>step</CODE> statement:
The <CODE>to</CODE> operator can be combined with a <CODE>step</CODE> statement:
<lang frink>
<syntaxhighlight lang="frink">
for i = 1 to 1000000 step 3
for i = 1 to 1000000 step 3
println[i]
println[i]
</syntaxhighlight>
</lang>


As a <CODE>foreach</CODE> statement. The <CODE>for</CODE> construct can iterate over the elements of an array, set, dictionary, or enumerating expression.
As a <CODE>foreach</CODE> statement. The <CODE>for</CODE> construct can iterate over the elements of an array, set, dictionary, or enumerating expression.
<lang frink>
<syntaxhighlight lang="frink">
for i = [2,3,7,9]
for i = [2,3,7,9]
println[i]
println[i]
</syntaxhighlight>
</lang>


===Do...While Loop===
===Do...While Loop===
<lang frink>
<syntaxhighlight lang="frink">
i=0
i=0
do
do
Line 408: Line 409:
i = i+1
i = i+1
} while i<1000
} while i<1000
</syntaxhighlight>
</lang>



==[[Groovy]]==
==[[Groovy]]==