<HTML> <HEAD> <!-- This HTML file has been created by texi2html 1.51 from ProgrammersManual.texinfo on 4 March 1997 --> <TITLE>LambdaMOO Programmer's Manual - Iteration</TITLE> </HEAD> <BODY> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_31.html">previous</A>, <A HREF="ProgrammersManual_33.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>. <P><HR><P> <H3><A NAME="SEC32" HREF="ProgrammersManual_toc.html#TOC32">Statements for Looping</A></H3> <P> MOO provides three different kinds of looping statements, allowing you to have a set of statements executed (1) once for each element of a given list, (2) once for each integer or object number in a given range, and (3) over and over until a given condition stops being true. </P> <P> To perform some statements once for each element of a given list, use this syntax: </P> <PRE> for <VAR>variable</VAR> in (<VAR>expression</VAR>) <VAR>statements</VAR> endfor </PRE> <P> The expression is evaluated and should return a list; if it does not, <CODE>E_TYPE</CODE> is raised. The <VAR>statements</VAR> are then executed once for each element of that list in turn; each time, the given <VAR>variable</VAR> is assigned the value of the element in question. For example, consider the following statements: </P> <PRE> odds = {1, 3, 5, 7, 9}; evens = {}; for n in (odds) evens = {@evens, n + 1}; endfor </PRE> <P> The value of the variable <SAMP>`evens'</SAMP> after executing these statements is the list </P> <PRE> {2, 4, 6, 8, 10} </PRE> <P> To perform a set of statements once for each integer or object number in a given range, use this syntax: </P> <PRE> for <VAR>variable</VAR> in [<VAR>expression-1</VAR>..<VAR>expression-2</VAR>] <VAR>statements</VAR> endfor </PRE> <P> The two expressions are evaluated in turn and should either both return integers or both return object numbers; <CODE>E_TYPE</CODE> is raised otherwise. The <VAR>statements</VAR> are then executed once for each integer (or object number, as appropriate) greater than or equal to the value of <VAR>expression-1</VAR> and less than or equal to the result of <VAR>expression-2</VAR>, in increasing order. Each time, the given variable is assigned the integer or object number in question. For example, consider the following statements: </P> <PRE> evens = {}; for n in [1..5] evens = {@evens, 2 * n}; endfor </PRE> <P> The value of the variable <SAMP>`evens'</SAMP> after executing these statements is just as in the previous example: the list </P> <PRE> {2, 4, 6, 8, 10} </PRE> <P> The following loop over object numbers prints out the number and name of every valid object in the database: </P> <PRE> for o in [#0..max_object()] if (valid(o)) notify(player, tostr(o, ": ", o.name)); endif endfor </PRE> <P> The final kind of loop in MOO executes a set of statements repeatedly as long as a given condition remains true: </P> <PRE> while (<VAR>expression</VAR>) <VAR>statements</VAR> endwhile </PRE> <P> The expression is evaluated and, if it returns a true value, the <VAR>statements</VAR> are executed; then, execution of the <SAMP>`while'</SAMP> statement begins all over again with the evaluation of the expression. That is, execution alternates between evaluating the expression and executing the statements until the expression returns a false value. The following example code has precisely the same effect as the loop just shown above: </P> <PRE> evens = {}; n = 1; while (n <= 5) evens = {@evens, 2 * n}; n = n + 1; endwhile </PRE> <BLOCKQUOTE> <P> <EM>Fine point:</EM> It is also possible to give a `name' to a <SAMP>`while'</SAMP> loop, using this syntax: <PRE> while <VAR>name</VAR> (<VAR>expression</VAR>) <VAR>statements</VAR> endwhile </PRE> <P> which has precisely the same effect as </P> <PRE> while (<VAR>name</VAR> = <VAR>expression</VAR>) <VAR>statements</VAR> endwhile </PRE> <P> This naming facility is only really useful in conjunction with the <SAMP>`break'</SAMP> and <SAMP>`continue'</SAMP> statements, described in the next section. </BLOCKQUOTE> <P> With each kind of loop, it is possible that the statements in the body of the loop will never be executed at all. For iteration over lists, this happens when the list returned by the expression is empty. For iteration on integers, it happens when <VAR>expression-1</VAR> returns a larger integer than <VAR>expression-2</VAR>. Finally, for the <SAMP>`while'</SAMP> loop, it happens if the expression returns a false value the very first time it is evaluated. </P> <P><HR><P> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_31.html">previous</A>, <A HREF="ProgrammersManual_33.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>. </BODY> </HTML>