| 1 | <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Smyčky</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Příručka k aplikaci Genius"><link rel="up" href="ch06.html" title="Chapter 6. Programování s jazykem GEL"><link rel="prev" href="ch06.html" title="Chapter 6. Programování s jazykem GEL"><link rel="next" href="ch06s03.html" title="Součty a součiny"></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">Smyčky</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Programování s jazykem 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>Smyčky</h2></div></div></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-while"></a>Smyčky while</h3></div></div></div><p lang="en"> |
| 2 | Syntax: |
| 3 | </p><pre lang="en" class="programlisting">while <expression1> do <expression2> |
| 4 | until <expression1> do <expression2> |
| 5 | do <expression2> while <expression1> |
| 6 | do <expression2> until <expression1></pre><p lang="en"> |
| 7 | |
| 8 | These are similar to other languages. However, as in GEL it is simply an expression that must have some return value, these |
| 9 | 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. |
| 10 | </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-for"></a>Smyčky for</h3></div></div></div><p lang="en"> |
| 11 | Syntax: |
| 12 | </p><pre lang="en" class="programlisting">for <identifier> = <from> to <to> do <body> |
| 13 | for <identifier> = <from> to <to> by <increment> do <body></pre><p lang="en"> |
| 14 | |
| 15 | 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: |
| 16 | </p><pre lang="en" class="programlisting">for i = 1 to 20 by 2 do print(i) |
| 17 | </pre><p lang="en"> |
| 18 | </p><p lang="en"> |
| 19 | When one of the values is a floating point number, then the |
| 20 | final check is done to within 2^-20 of the step size. That is, |
| 21 | even if we overshoot by 2^-20 times the "by" above, we still execute the last |
| 22 | iteration. This way |
| 23 | </p><pre lang="en" class="programlisting">for x = 0 to 1 by 0.1 do print(x) |
| 24 | </pre><p lang="en"> |
| 25 | 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 |
| 26 | 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 |
| 27 | the majority of the cases. If you want to avoid dealing with this issue, use actual rational numbers for example: |
| 28 | </p><pre lang="en" class="programlisting">for x = 0 to 1 by 1/10 do print(x) |
| 29 | </pre><p lang="en"> |
| 30 | This check is done only from version 1.0.16 onwards, so execution of your code may differ on older versions. |
| 31 | </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-foreach"></a>Smyčky foreach</h3></div></div></div><p lang="en"> |
| 32 | Syntax: |
| 33 | </p><pre lang="en" class="programlisting">for <identifier> in <matrix> do <body></pre><p lang="en"> |
| 34 | |
| 35 | For each element in the matrix, going row by row from left to right we execute the body |
| 36 | with the identifier set to the current element. To |
| 37 | print numbers 1,2,3 and 4 in this order you could do: |
| 38 | </p><pre lang="en" class="programlisting">for n in [1,2:3,4] do print(n) |
| 39 | </pre><p lang="en"> |
| 40 | If you wish to run through the rows and columns of a matrix, you can use |
| 41 | the RowsOf and ColumnsOf functions, which return a vector of the rows or |
| 42 | columns of the matrix. So, |
| 43 | </p><pre lang="en" class="programlisting">for n in RowsOf ([1,2:3,4]) do print(n) |
| 44 | </pre><p lang="en"> |
| 45 | will print out [1,2] and then [3,4]. |
| 46 | </p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="genius-gel-loops-break-continue"></a>Break a continue</h3></div></div></div><p lang="en"> |
| 47 | 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. |
| 48 | </p><pre lang="en" class="programlisting">while(<expression1>) do ( |
| 49 | if(<expression2>) break |
| 50 | else if(<expression3>) continue; |
| 51 | <expression4> |
| 52 | ) |
| 53 | </pre><p lang="en"> |
| 54 | </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. Programování s jazykem GEL </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Součty a součiny</td></tr></table></div></body></html> |