Files
wren/branching.html
2015-01-01 21:04:14 -08:00

130 lines
9.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Branching Wren</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700,400italic,700italic|Source+Code+Pro:400|Lato:400|Sanchez:400italic,400' rel='stylesheet' type='text/css'>
<!-- Tell mobile browsers we're optimized for them and they don't need to crop
the viewport. -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
</head>
<body id="top">
<header>
<div class="page">
<div class="main-column">
<h1><a href="index.html">wren</a></h1>
<h2>a classy little scripting language</h2>
</div>
</div>
</header>
<div class="page">
<nav>
<ul>
<li><a href="getting-started.html">Getting Started</a></li>
</ul>
<section>
<h2>language</h2>
<ul>
<li><a href="syntax.html">Syntax</a></li>
<li><a href="method-calls.html">Method Calls</a></li>
<li><a href="variables.html">Variables</a></li>
<li><a href="branching.html">Branching</a></li>
<li><a href="looping.html">Looping</a></li>
<li><a href="error-handling.html">Error Handling</a></li>
</ul>
</section>
<section>
<h2>types</h2>
<ul>
<li><a href="values.html">Values</a></li>
<li><a href="classes.html">Classes</a></li>
<li><a href="fibers.html">Fibers</a></li>
<li><a href="functions.html">Functions</a></li>
<li><a href="lists.html">Lists</a></li>
<li><a href="maps.html">Maps</a></li>
</ul>
</section>
<section>
<h2>reference</h2>
<ul>
<li><a href="core-library.html">Core Library</a></li>
<li><a href="embedding-api.html">Embedding API</a></li>
<li><a href="performance.html">Performance</a></li>
<li><a href="contributing.html">Contributing</a></li>
<li><a href="qa.html">Q &amp; A</a></li>
</ul>
</section>
</nav>
<main>
<h1>Branching</h1>
<p><em>Control flow</em> is used to determine which chunks of code are executed and how many times. Expressions and statements for deciding whether or not to execute some code are called <em>branching</em> and are covered here. To execute something more than once, you'll want <a href="looping.html"><em>looping</em></a>.</p>
<h2>Truth <a href="#truth" name="truth" class="header-anchor">#</a></h2>
<p>Branching is conditional on the value of some expression. We take the entire universe of possible values and divide them into two buckets: some we consider "true" and the rest are "false". If the expression results in a value in the true bucket, we branch one way. Otherwise, we go the other way.</p>
<p>Obviously, the boolean <code>true</code> is in the "true" bucket and <code>false</code> is in "false", but what about values of other types? The choice is ultimately arbitrary, and different languages have different rules. Wren's rules follow Ruby:</p>
<ul>
<li>The boolean value <code>false</code> is false.</li>
<li>The null value <code>null</code> is false.</li>
<li>Everything else is true.</li>
</ul>
<p>This means <code>0</code>, empty strings, and empty collections are all considered "true" values.</p>
<h2>If statements <a href="#if-statements" name="if-statements" class="header-anchor">#</a></h2>
<p>The simplest branching statement, <code>if</code> lets you conditionally skip a chunk of code. It looks like this:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="n">ready</span><span class="p">)</span> <span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;go!&quot;</span><span class="p">)</span>
</pre></div>
<p>That evaluates the parenthesized expression after <code>if</code>. If it's true, then the statement after the condition is evaluated. Otherwise it is skipped. Instead of a statement, you can have a <a href="syntax.html#blocks">block</a>:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="n">ready</span><span class="p">)</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;getSet&quot;</span><span class="p">)</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;go!&quot;</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<p>You may also provide an <code>else</code> branch. It will be executed if the condition is false:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="n">ready</span><span class="p">)</span> <span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;go!&quot;</span><span class="p">)</span> <span class="k">else</span> <span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;not ready!&quot;</span><span class="p">)</span>
</pre></div>
<p>And, of course, it can take a block too:</p>
<div class="codehilite"><pre><span class="k">if</span> <span class="p">(</span><span class="n">ready</span><span class="p">)</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;go!&quot;</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="s2">&quot;not ready!&quot;</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<h2>The logical operators <code>&amp;&amp;</code> and <code>||</code> <a href="#the-logical-operators-<code>&amp;&amp;</code>-and-<code>||</code>" name="the-logical-operators-<code>&amp;&amp;</code>-and-<code>||</code>" class="header-anchor">#</a></h2>
<p>The <code>&amp;&amp;</code> and <code>||</code> operators are lumped here under branching because they conditionally execute some code&mdash;they short-circuit. Both of them are infix operators, and, depending on the value of the left-hand side, the right-hand operand expression may or may not be evaluated.</p>
<p>An <code>&amp;&amp;</code> ("logical and") expression evaluates the left-hand argument. If it's false, it returns that value. Otherwise it evaluates and returns the right-hand argument.</p>
<div class="codehilite"><pre><span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="kc">false</span> <span class="o">&amp;&amp;</span> <span class="m">1</span><span class="p">)</span> <span class="c1">// false</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="m">1</span> <span class="o">&amp;&amp;</span> <span class="m">2</span><span class="p">)</span> <span class="c1">// 2</span>
</pre></div>
<p>An <code>||</code> ("logical or") expression is reversed. If the left-hand argument is true, it's returned, otherwise the right-hand argument is evaluated and returned:</p>
<div class="codehilite"><pre><span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="kc">false</span> <span class="o">||</span> <span class="m">1</span><span class="p">)</span> <span class="c1">// 1</span>
<span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="m">1</span> <span class="o">||</span> <span class="m">2</span><span class="p">)</span> <span class="c1">// 1</span>
</pre></div>
<h2>The conditional operator <code>?:</code> <a href="#the-conditional-operator-<code>" name="the-conditional-operator-</code>" class="header-anchor">#</a></h2>
<p>Also known as the "ternary" operator since it takes three arguments, Wren has the little "if statement in the form of an expression" you know and love from C and its bretheren.</p>
<div class="codehilite"><pre><span class="n">IO</span><span class="p">.</span><span class="n">print</span><span class="p">(</span><span class="m">1</span> <span class="o">!=</span> <span class="m">2</span> <span class="o">?</span> <span class="s2">&quot;math is sane&quot;</span> <span class="o">:</span> <span class="s2">&quot;math is not sane!&quot;</span><span class="p">)</span>
</pre></div>
<p>It takes a condition expression, followed by <code>?</code>, followed by a then expression, a <code>:</code>, then an else expression. Just like <code>if</code>, it evaluates the condition. If true, it evaluates (and returns) the then expression. Otherwise it does the else expression.</p>
</main>
</div>
<footer>
<div class="page">
<div class="main-column">
<p>Wren lives <a href="https://github.com/munificent/wren">on GitHub</a> &mdash; Made with &#x2764; by <a href="http://journal.stuffwithstuff.com/">Bob Nystrom</a>.</p>
<div class="main-column">
</div>
</footer>
</body>
</html>