<?xml version="1.0" encoding="utf-8" ?> <!-- for emacs: -*- coding: utf-8 -*- --> <!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head><title>for -- for loops</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_mapping_spover_splists.html">next</a> | <a href="_while.html">previous</a> | <a href="_mapping_spover_splists.html">forward</a> | <a href="_while.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div> </td> </tr> </table> <div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_for.html" title="for loops">for</a></div> <hr/> <div><h1>for -- for loops</h1> <div class="single"><h2>Synopsis</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>for i from m to n when p list x do z</tt></div> </dd></dl> </div> </li> <li><div class="single">Inputs:<ul><li><span><tt>m</tt>, <span>an <a href="___Z__Z.html">integer</a></span></span></li> <li><span><tt>n</tt>, <span>an <a href="___Z__Z.html">integer</a></span></span></li> </ul> </div> </li> <li><div class="single">Consequences:<ul><li>The variable <tt>i</tt> is initialized to <tt>m</tt>. As long as <tt>i</tt> is not greater than <tt>n</tt>, evaluation of the loop continues. First <tt>p</tt> is evaluated. As long as the value of <tt>p</tt> is true, evaluation of the loop continues. Next <tt>x</tt> is evaluated and its value is saved, and <tt>z</tt> is evaluated and its value is discarded. Then <tt>i</tt> is incremented by 1, and the loop repeats. When the value of <tt>p</tt> is false, then the loop terminates, and the list of values of <tt>x</tt> is returned as the value of the entire expression.</li> </ul> </div> </li> <li><div class="single">Outputs:<ul><li><span>the list of values of the clause <tt>x</tt>, as desribed above</span></li> </ul> </div> </li> </ul> </div> <div class="single"><h2>Description</h2> <div><div><h2>Synopsis</h2> <ul><li><div class="list"><dl class="element"><dt class="heading">Usage: </dt><dd class="value"><div><tt>for i in v when p list x do z</tt></div> </dd></dl> </div> </li> <li>Inputs:<ul><li><span><tt>v</tt>, <span>a <a href="___Basic__List.html">basic list</a></span></span></li> </ul> </li> <li>Consequences:<ul><li>The variable <tt>i</tt> is set to consecutive values of the list <tt>v</tt>. First <tt>p</tt> is evaluated. As long as the value of <tt>p</tt> is true, evaluation of the loop continues. Next <tt>x</tt> is evaluated, and its value is saved. Then <tt>z</tt> is evaluated and its value is discarded. Then the loop repeats with the next element of <tt>v</tt>. When the value of <tt>p</tt> is false, then the loop terminates, and the list of values of <tt>x</tt> is returned as the value of the entire expression.</li> </ul> </li> </ul> </div> <h2>examples</h2> <table class="examples"><tr><td><pre>i1 : for i from 1 to 5 when i < 15 list i^2 do print i 1 2 3 4 5 o1 = {1, 4, 9, 16, 25} o1 : List</pre> </td></tr> <tr><td><pre>i2 : for i from 1 to 5 when i^2 < 15 list i^2 do print i 1 2 3 o2 = {1, 4, 9} o2 : List</pre> </td></tr> </table> <p>The <tt>do z</tt> clause may be omitted.</p> <table class="examples"><tr><td><pre>i3 : for i from 1 to 5 when i < 15 list i^2 o3 = {1, 4, 9, 16, 25} o3 : List</pre> </td></tr> </table> <p>The <tt>from m</tt> clause may be omitted, in which case <tt>i</tt> starts with <tt>0</tt>.</p> <table class="examples"><tr><td><pre>i4 : for i to 5 when i < 15 list i^2 o4 = {0, 1, 4, 9, 16, 25} o4 : List</pre> </td></tr> </table> <p>The <tt>when p</tt> clause may be omitted.</p> <table class="examples"><tr><td><pre>i5 : for i to 5 list i^2 o5 = {0, 1, 4, 9, 16, 25} o5 : List</pre> </td></tr> </table> <p>The <tt>to n</tt> clause may be omitted.</p> <table class="examples"><tr><td><pre>i6 : for i when i < 15 list i^2 o6 = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196} o6 : List</pre> </td></tr> </table> <p>The <tt>list x</tt> clause may be omitted.</p> <table class="examples"><tr><td><pre>i7 : for i when i^2 < 15 do print i 0 1 2 3</pre> </td></tr> </table> <p>If <a href="_continue.html" title="continue with the next iteration of a loop">continue</a> is executed by <tt>x</tt> then execution of <tt>x</tt> is interrupted, no value is added to the list, and iteration of the loop continues.</p> <table class="examples"><tr><td><pre>i8 : for i from 0 when i < 10 list (if odd i then continue; i^2) o8 = {0, 4, 16, 36, 64} o8 : List</pre> </td></tr> </table> <p>If <tt>continue w</tt> is executed by <tt>x</tt> then execution of <tt>x</tt> is interrupted, the value of <tt>w</tt> is added to the list, and iteration of the loop continues.</p> <table class="examples"><tr><td><pre>i9 : for i from 0 when i < 10 list (if odd i then continue x; i^2) o9 = {0, x, 4, x, 16, x, 36, x, 64, x} o9 : List</pre> </td></tr> </table> <p> If <tt>break v</tt> is executed by <tt>x</tt>, then the loop is stopped and <tt>v</tt> is returned as its value.</p> <table class="examples"><tr><td><pre>i10 : for i from 0 when i < 10 list (if i== 5 then break i; i^2) o10 = 5</pre> </td></tr> </table> <p>If <a href="_break.html" title="break from a loop">break</a> is executed by <tt>x</tt>, then the loop is stopped and the list accumulated so far is returned as the value.</p> <table class="examples"><tr><td><pre>i11 : for i from 0 when i < 10 list (if i== 5 then break; i^2) o11 = {0, 1, 4, 9, 16} o11 : List</pre> </td></tr> </table> <p>The variable <tt>i</tt> is a new local variable whose scope includes only the expressions <tt>p</tt>, <tt>x</tt>, and <tt>y</tt>. The numbers <tt>m</tt> and <tt>n</tt> must be small integers that fit into a single word.</p> <table class="examples"><tr><td><pre>i12 : for i in 0..3 list i^2 o12 = {0, 1, 4, 9} o12 : List</pre> </td></tr> </table> </div> </div> <div class="waystouse"><h2>For the programmer</h2> <p>The object <a href="_for.html" title="for loops">for</a> is <span>a <a href="___Keyword.html">keyword</a></span>.</p> </div> </div> </body> </html>