Games-Axmud
view release on metacpan or search on metacpan
share/docs/tutorial/ch05.mkd view on Meta::CPAN
bilbobaggins
Before using an expression, Axbasic always works out the answer. We call this *evaluating the expression*. In this example, Axbasic evaluates the expression first, *before* PRINTing anything. The script PRINTs the number 15; it doesn't PRINT the numb...
PRINT 1 + 2 + 3 + 4 + 5
END
##<a name="5.9">5.9 Operator precedence</a>
Take a look at the following script, and try to work out which number it PRINTs.
PRINT 1 + 2 * 4
END
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.
All programming languages - including Axbasic - have some notion of *operator precedence*, which is a fancy way of saying that multiplication should be done before addition.
In other words, for the expression:
1 + 2 * 4
We do the multiplication *first*, which gives us:
1 + 8
*Then* we do the addition, giving us the final value of 9. Axbasic *always* gives this same value, no matter how many times you run the script.
(For anyone who is wondering, operators have *left-to-right associativity*. Because there are comparitively few operators compared to more modern languages, associativity is irrelevant in the behaviour of Axbasic scripts.)
##<a name="5.10">5.10 Using brackets</a>
At this point, all programming tutorials will tell you that writing an expression like **1 + 2 * 4** is a *really bad idea*, even though it's perfectly legal Axbasic.
A much better way is to use brackets. In an expression, everything inside a pair of brackets is worked out first.
(1 + 2) * 4
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.
It's possible to use brackets within brackets, if you need to.
( (1 + 2) * 4 ) / 2
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.
You can use as many brackets as you want, but the expression must contain exactly the same number of open brackets and close brackets.
##<a name="5.11">5.11 Getting input</a>
One way to set a variable's value is to use LET. Another way is to ask the user to type the value themselves.
An INPUT statement waits for the user to type something, and then stores it in a variable
PRINT "What is your name?"
INPUT name$
PRINT "Your name is"
PRINT name$
END
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 **name$**.
##<a name="5.12">5.12 Avoiding LET</a>
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:
number = 10
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:
OPTION NOLET
Now you don't need to use LET at all.
OPTION NOLET
first = 10
second = 5
third = 2
PRINT first * second * third
END
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.
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 it.)
##<a name="5.13">5.13 Illegal expressions</a>
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 *undefined*.)
If you try running this script, you'll get a *Division by zero* error.
PRINT 10 / 0
END
However, multiplying by zero or raising a number to the power of zero *is* allowed. (The results are always zero and one, respectively.)
---
[Previous](ch04.html) [Index](index.html) [Next](ch06.html)
( run in 1.121 second using v1.01-cache-2.11-cpan-364913b4093 )