<HTML> <HEAD> <!-- This HTML file has been created by texi2html 1.51 from ProgrammersManual.texinfo on 4 March 1997 --> <TITLE>LambdaMOO Programmer's Manual - Conditionals</TITLE> </HEAD> <BODY> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_30.html">previous</A>, <A HREF="ProgrammersManual_32.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="SEC31" HREF="ProgrammersManual_toc.html#TOC31">Statements for Testing Conditions</A></H3> <P> The <SAMP>`if'</SAMP> statement allows you to decide whether or not to perform some statements based on the value of an arbitrary expression: </P> <PRE> if (<VAR>expression</VAR>) <VAR>statements</VAR> endif </PRE> <P> <VAR>Expression</VAR> is evaluated and, if it returns a true value, the statements are executed in order; otherwise, nothing more is done. </P> <P> One frequently wants to perform one set of statements if some condition is true and some other set of statements otherwise. The optional <SAMP>`else'</SAMP> phrase in an <SAMP>`if'</SAMP> statement allows you to do this: </P> <PRE> if (<VAR>expression</VAR>) <VAR>statements-1</VAR> else <VAR>statements-2</VAR> endif </PRE> <P> This statement is executed just like the previous one, except that <VAR>statements-1</VAR> are executed if <VAR>expression</VAR> returns a true value and <VAR>statements-2</VAR> are executed otherwise. </P> <P> Sometimes, one needs to test several conditions in a kind of nested fashion: </P> <PRE> if (<VAR>expression-1</VAR>) <VAR>statements-1</VAR> else if (<VAR>expression-2</VAR>) <VAR>statements-2</VAR> else if (<VAR>expression-3</VAR>) <VAR>statements-3</VAR> else <VAR>statements-4</VAR> endif endif endif </PRE> <P> Such code can easily become tedious to write and difficult to read. MOO provides a somewhat simpler notation for such cases: </P> <PRE> if (<VAR>expression-1</VAR>) <VAR>statements-1</VAR> elseif (<VAR>expression-2</VAR>) <VAR>statements-2</VAR> elseif (<VAR>expression-3</VAR>) <VAR>statements-3</VAR> else <VAR>statements-4</VAR> endif </PRE> <P> Note that <SAMP>`elseif'</SAMP> is written as a single word, without any spaces. This simpler version has the very same meaning as the original: evaluate <VAR>expression-i</VAR> for <VAR>i</VAR> equal to 1, 2, and 3, in turn, until one of them returns a true value; then execute the <VAR>statements-i</VAR> associated with that expression. If none of the <VAR>expression-i</VAR> return a true value, then execute <VAR>statements-4</VAR>. </P> <P> Any number of <SAMP>`elseif'</SAMP> phrases can appear, each having this form: </P> <PRE> elseif (<VAR>expression</VAR>) <VAR>statements</VAR> </PRE> <P> The complete syntax of the <SAMP>`if'</SAMP> statement, therefore, is as follows: </P> <PRE> if (<VAR>expression</VAR>) <VAR>statements</VAR> <VAR>zero-or-more-elseif-phrases</VAR> <VAR>an-optional-else-phrase</VAR> endif </PRE> <P><HR><P> Go to the <A HREF="ProgrammersManual_1.html">first</A>, <A HREF="ProgrammersManual_30.html">previous</A>, <A HREF="ProgrammersManual_32.html">next</A>, <A HREF="ProgrammersManual_77.html">last</A> section, <A HREF="ProgrammersManual_toc.html">table of contents</A>. </BODY> </HTML>