<?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>conditional execution -- if-then-else statements</title> <link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/> </head> <body> <table class="buttons"> <tr> <td><div><a href="_while.html">next</a> | <a href="_operator__Attributes.html">previous</a> | <a href="_while.html">forward</a> | <a href="_operators.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="_conditional_spexecution.html" title="if-then-else statements">conditional execution</a></div> <hr/> <div><h1>conditional execution -- if-then-else statements</h1> <div>The basic way to control the execution of code is with the <a href="_if.html" title="condition testing">if</a> expression. Such an expression typically has the form <pre>if X then Y else Z</pre> and is evaluated as follows. First <tt>X</tt> is evaluated. If the result is <tt>true</tt>, then the value of <tt>Y</tt> is provided, and if the result is <tt>false</tt>, then the value of <tt>Z</tt> is provided. An error is signalled if the value of <tt>X</tt> is anything but <tt>true</tt> or <tt>false</tt>.<table class="examples"><tr><td><pre>i1 : (-4 .. 4) / (i -> if i < 0 then "neg" else if i == 0 then "zer" else "pos") o1 = (neg, neg, neg, neg, zer, pos, pos, pos, pos) o1 : Sequence</pre> </td></tr> </table> The else clause may be omitted from an <tt>if</tt> expression. In that case, if value of the predicate <tt>X</tt> is false, then <a href="_null.html" title="the unique member of the empty class">null</a> is provided as the value of the <tt>if</tt> expression.<table class="examples"><tr><td><pre>i2 : (-4 .. 4) / (i -> if i < 0 then "neg" else if i == 0 then "zer") o2 = (neg, neg, neg, neg, zer, , , , ) o2 : Sequence</pre> </td></tr> </table> There are a variety of predicate functions (such as <tt><</tt>, used above) that yield <tt>true</tt> or <tt>false</tt> and can be used as the predicate in an <tt>if</tt> expression. For a list, see <a href="___Boolean.html" title="the class of Boolean values">Boolean</a>. Boolean results may be combined with <a href="_not.html" title="negation">not</a>, <a href="_and.html" title="conjunction">and</a>, and <a href="_or.html" title="disjunction">or</a>.</div> </div> </body> </html>