Welcome to the second part of our beginners introduction to programming the
FirST STeps in BASIC
Doing simple maths using a program like PowerBASIC is not so very different
from using a calculator. One problem that arises is the ST symbols for
operations often bear no resemblance to those use in normal mathematical
notation. The table below lists the most common symbols:
A ^ B Raise A to the Bth power
A * B Multiply A by B
A / B Divide A by B
A MOD B Divide A by B and leave the remainder
A + B Add A to B
A - B Subtract B away from A
In BASIC "MOD" stands for modulus, and is a keyword not a symbol. Because of
the way BASIC works, this make no difference to the way the equation works.
The tilde ( ^ ) raises a number to the specified power, and is called
exponentiation. Try experimenting with the symbols and keywords above to see
what results you get.
At this point, you may notice that the equals sign ( = ) is not included
anywhere in the table. Although equals in often used in BASIC, it is not
required if we want an immediate result.
With the above table firmly in mind, lets try some simple sums. First, enter:
(Remember to press return after each line. Select RUN from menu to see the
result, or press ALT-R)
The ST replies with 5, a correct answer. Now try:
The ST prints the answer as 8. This sum is interesting because there are two
different answers, depending on how you work it out.
2+3 equals 5
5*2 equals 10
Final answer: 10
3*2 equals 6
6+2 equals 8
Final answer: 8
How the result comes out is determined by how the numbers of the problem are
tackled. If you simply solve the problem from first to last, taking each
number and symbol in order then the answer is 10. If you use the MDAS rule,
then the answer comes out as 8
MY DEAR AUNT SALLY...?
My Dear Aunt Sally is the way children are often taught to remember this
mathematical rule in school. Like so many things in maths, there is a correct
order to tackle solving a problem, like this:
and Subtraction last
This is the order we are taught to evaluate sums in school, and is the way all
maths problems should be solved. Because the rule applies to the mathematical
operations, it is called "OPERATOR PRECEDENCE". In our example, PowerBASIC
computes the multiplication first, then the addition.
There are often times when it is useful to force BASIC to evaluate an
expression in a particular order, say we want to do the addition first. The
way to do this is to place part of the expression in brackets, forcing BASIC
to work out that part first.
In the previous example, if we wanted to force BASIC to come up with an answer
of 10, we need to write the expression like this:
BASIC works out the bracketed part of the expression first and get and answer
of 5. It then multiplies this by 2 to get the required answer, in this case
If necessary, brackets can be used to create some needlessly complex
equations, like this:
In practice, nesting brackets to this sort of level is very unusual. For a
start it makes the equation practically impossible to read, let alone
understand. Errors tend to crop up, such as not having enough opening or
closing brackets, or getting the brackets in the wrong place. If you need a
create an equation as complex as this, it is often possible to break it down
into several simpler steps.
UNTIL NEXT TIME...
As a little practice with numbers, try modifying the simple equations used
here, changing the +, -, / and * signs and try to predict the results. Also
see what happens when brackets are used. Try creating equations using
brackets, and put the brackets in different places to get a different result.
In the next part of the series I'll be looking at how programs can use
variables to store numbers, and how those variables can be addressed by name.
Marko, Suomen Atari-sivut / ArkiSTo 2003