<?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>while -- while loops</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_for.html">next</a> | <a href="_conditional_spexecution.html">previous</a> | <a href="_for.html">forward</a> | <a href="_conditional_spexecution.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="_while.html" title="while loops">while</a></div> <hr/> <div><h1>while -- while 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>while p list x do z</tt></div> </dd></dl> </div> </li> <li><div class="single">Consequences:<ul><li>The expression <tt>p</tt> is repeatedly evaluated. As long as the result is true, <tt>x</tt> is evaluated and its value is saved, and <tt>z</tt> is evaluated and its value is discarded. 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> </ul> </div> <div class="single"><h2>Description</h2> <div><table class="examples"><tr><td><pre>i1 : i = 0 ; while i < 10 list i^2 do i = i+1 o2 = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} o2 : List</pre> </td></tr> </table> <p>The <tt>list x</tt> clause may be omitted, in which case no list is accumulated, and <a href="_null.html" title="the unique member of the empty class">null</a> is returned as the value of the expression.</p> <table class="examples"><tr><td><pre>i3 : i = 0 ; while i < 4 do (print i; i = i+1) 0 1 2 3</pre> </td></tr> </table> <p>Alternatively, the <tt>do z</tt> clause may be omitted.</p> <table class="examples"><tr><td><pre>i5 : i = 0 ; while i < 10 list (i = i+1; i^2) o6 = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} o6 : List</pre> </td></tr> </table> <p>Observe the use of the semicolon (see <tt>;</tt>) in the expression above.</p> <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>i7 : i = 0 ; while i < 10 list (i = i+1; if odd i then continue; i^2) o8 = {4, 16, 36, 64, 100} 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 : i = 0 ; while i < 10 list (i = i+1; if odd i then continue x; i^2) o10 = {x, 4, x, 16, x, 36, x, 64, x, 100} o10 : 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>i11 : i = 0 ; while i < 10 list (i = i+1; if i == 5 then break i; i^2) o12 = 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>i13 : i = 0 ; while i < 10 list (i = i+1; if i == 5 then break; i^2) o14 = {1, 4, 9, 16} o14 : List</pre> </td></tr> </table> </div> </div> <div class="waystouse"><h2>For the programmer</h2> <p>The object <a href="_while.html" title="while loops">while</a> is <span>a <a href="___Keyword.html">keyword</a></span>.</p> </div> </div> </body> </html>