Games-Axmud
view release on metacpan or search on metacpan
share/docs/tutorial/ch05.html view on Meta::CPAN
<p>The output looks like this:</p>
<pre><code> 4
8
1024
</code></pre>
<h2><a name="5.8">5.8 Expressions</a></h2>
<p>The characters we've just used ( & + - * / and ^ ) are called <em>operators</em>.</p>
<p>When we put a series of values and operators together, we get an <em>expression</em>. All of the following lines are expressions.</p>
<pre><code> 42
1 + 2
10 - 5 + 1
3 * 6 * 2
"bilbo" & "baggins"
</code></pre>
<p>The important thing to remember about expressions is that they can be reduced to a single value. In other words, we can work out the "answer".</p>
<pre><code> 42
3
6
36
bilbobaggins
</code></pre>
<p>Before using an expression, Axbasic always works out the answer. We call this <em>evaluating the expression</em>. In this example, Axbasic evaluates the expression first, <em>before</em> PRINTing anything. The script PRINTs the number 15; it doesn...
<pre><code> PRINT 1 + 2 + 3 + 4 + 5
END
</code></pre>
<h2><a name="5.9">5.9 Operator precedence</a></h2>
<p>Take a look at the following script, and try to work out which number it PRINTs.</p>
<pre><code> PRINT 1 + 2 * 4
END
</code></pre>
<p>If you think the value is 9, you're right. But you might have thought the script would display 12; after all, 1 plus 2 is 3, and 3 multiplied by 4 is 12.</p>
<p>All programming languages - including Axbasic - have some notion of <em>operator precedence</em>, which is a fancy way of saying that multiplication should be done before addition.</p>
<p>In other words, for the expression:</p>
<pre><code> 1 + 2 * 4
</code></pre>
<p>We do the multiplication <em>first</em>, which gives us:</p>
<pre><code> 1 + 8
</code></pre>
<p><em>Then</em> we do the addition, giving us the final value of 9. Axbasic <em>always</em> gives this same value, no matter how many times you run the script.</p>
<p>(For anyone who is wondering, operators have <em>left-to-right associativity</em>. Because there are comparitively few operators compared to more modern languages, associativity is irrelevant in the behaviour of Axbasic scripts.)</p>
<h2><a name="5.10">5.10 Using brackets</a></h2>
<p>At this point, all programming tutorials will tell you that writing an expression like <strong>1 + 2 * 4</strong> is a <em>really bad idea</em>, even though it's perfectly legal Axbasic.</p>
<p>A much better way is to use brackets. In an expression, everything inside a pair of brackets is worked out first.</p>
<pre><code> (1 + 2) * 4
</code></pre>
<p>In this example, 1 plus 2 is 3. We work that part out first because it's inside a pair of brackets. Then we work out that 3 multiplied by 4 is 12.</p>
<p>It's possible to use brackets within brackets, if you need to.</p>
<pre><code> ( (1 + 2) * 4 ) / 2
</code></pre>
<p>In this example, we work out the innermost sum first. 1 plus 2 is 3, and 3 multipled by 4 is 12. Then we divide 12 by 2, to get the final answer of 6.</p>
<p>You can use as many brackets as you want, but the expression must contain exactly the same number of open brackets and close brackets.</p>
<h2><a name="5.11">5.11 Getting input</a></h2>
<p>One way to set a variable's value is to use LET. Another way is to ask the user to type the value themselves.</p>
<p>An INPUT statement waits for the user to type something, and then stores it in a variable</p>
<pre><code> PRINT "What is your name?"
INPUT name$
PRINT "Your name is"
PRINT name$
END
</code></pre>
<p>Axbasic creates a dialogue (popup) window. After the user types their name and presses ENTER, the window disappears. The user's response is stored in the variable <strong>name$</strong>.</p>
<h2><a name="5.12">5.12 Avoiding LET</a></h2>
<p>After a while, BASIC programmers grow weary of typing LET every time they want to set a variable, and begin wondering why they can't just type something like this:</p>
<pre><code> number = 10
</code></pre>
<p>Well, in fact it's possible to do tell Axbasic that you don't want to use LET at all. Just put this line somewhere in your script:</p>
<pre><code> OPTION NOLET
</code></pre>
<p>Now you don't need to use LET at all.</p>
<pre><code> OPTION NOLET
first = 10
second = 5
third = 2
PRINT first * second * third
END
</code></pre>
<p>By the way, before Axbasic starts executing a script, it checks every line, looking for OPTION statements. If it finds any, it applies them immediately.</p>
<p>In other words, your OPTION NOLET can appear anywhere in the script. In practice, though, it's a very bad idea to put it anywhere but at the beginning. (Your computer doesn't mind checking every line in the script, but humans won't thank you for i...
<h2><a name="5.13">5.13 Illegal expressions</a></h2>
<p>In Axbasic, as in all other programming languages, you can't divide any number by zero. This simply isn't allowed. (A mathematician would say the operation is <em>undefined</em>.)</p>
<p>If you try running this script, you'll get a <em>Division by zero</em> error.</p>
<pre><code> PRINT 10 / 0
END
</code></pre>
<p>However, multiplying by zero or raising a number to the power of zero <em>is</em> allowed. (The results are always zero and one, respectively.)</p>
<hr>
<p><a href="ch04.html">Previous</a> <a href="index.html">Index</a> <a href="ch06.html">Next</a></p>
</body>
</html>
( run in 0.847 second using v1.01-cache-2.11-cpan-364913b4093 )