1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Loops</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Genius Manual"><link rel="up" href="ch06.html" title="Chapter 6. Programming with GEL"><link rel="prev" href="ch06.html" title="Chapter 6. Programming with GEL"><link rel="next" href="ch06s03.html" title="Sums and Products"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Loops</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Programming with GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch06s03.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-loops"></a>Loops</h2></div></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-while"></a>While Loops</h3></div></div></div><p>
Syntax:
</p><pre class="programlisting">while <expression1> do <expression2>
until <expression1> do <expression2>
do <expression2> while <expression1>
do <expression2> until <expression1></pre><p>
These are similar to other languages. However, as in GEL it is simply an expression that must have some return value, these
constructs will simply return the result of the last iteration or <code class="literal">NULL</code> if no iteration was done. In the boolean expression, <code class="literal">=</code> is translated into <code class="literal">==</code> just as for the <code class="literal">if</code> statement.
</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-for"></a>For Loops</h3></div></div></div><p>
Syntax:
</p><pre class="programlisting">for <identifier> = <from> to <to> do <body>
for <identifier> = <from> to <to> by <increment> do <body></pre><p>
Loop with identifier being set to all values from <code class="literal"><from></code> to <code class="literal"><to></code>, optionally using an increment other than 1. These are faster, nicer and more compact than the normal loops such as above, but less flexible. The identifier must be an identifier and can't be a dereference. The value of identifier is the last value of identifier, or <code class="literal"><from></code> if body was never evaluated. The variable is guaranteed to be initialized after a loop, so you can safely use it. Also the <code class="literal"><from></code>, <code class="literal"><to></code> and <code class="literal"><increment></code> must be non complex values. The <code class="literal"><to></code> is not guaranteed to be hit, but will never be overshot, for example the following prints out odd numbers from 1 to 19:
</p><pre class="programlisting">for i = 1 to 20 by 2 do print(i)
</pre><p>
</p><p>
When one of the values is a floating point number, then the
final check is done to within 2^-20 of the step size. That is,
even if we overshoot by 2^-20 times the "by" above, we still execute the last
iteration. This way
</p><pre class="programlisting">for x = 0 to 1 by 0.1 do print(x)
</pre><p>
does the expected even though adding 0.1 ten times becomes just slightly more than 1.0 due to the way that floating point numbers
are stored in base 2 (there is no 0.1, the actual number stored is just ever so slightly bigger). This is not perfect but it handles
the majority of the cases. If you want to avoid dealing with this issue, use actual rational numbers for example:
</p><pre class="programlisting">for x = 0 to 1 by 1/10 do print(x)
</pre><p>
This check is done only from version 1.0.16 onwards, so execution of your code may differ on older versions.
</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-foreach"></a>Foreach Loops</h3></div></div></div><p>
Syntax:
</p><pre class="programlisting">for <identifier> in <matrix> do <body></pre><p>
For each element in the matrix, going row by row from left to right we execute the body
with the identifier set to the current element. To
print numbers 1,2,3 and 4 in this order you could do:
</p><pre class="programlisting">for n in [1,2:3,4] do print(n)
</pre><p>
If you wish to run through the rows and columns of a matrix, you can use
the RowsOf and ColumnsOf functions, which return a vector of the rows or
columns of the matrix. So,
</p><pre class="programlisting">for n in RowsOf ([1,2:3,4]) do print(n)
</pre><p>
will print out [1,2] and then [3,4].
</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-break-continue"></a>Break and Continue</h3></div></div></div><p>
You can also use the <code class="literal">break</code> and <code class="literal">continue</code> commands in loops. The continue <code class="literal">continue</code> command will restart the current loop at its next iteration, while the <code class="literal">break</code> command exits the current loop.
</p><pre class="programlisting">while(<expression1>) do (
if(<expression2>) break
else if(<expression3>) continue;
<expression4>
)
</pre><p>
</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. Programming with GEL </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Sums and Products</td></tr></table></div></body></html>
|