Line 40 contains an example
of
a valid expression containing arithmetic operators.
Here are some more:
a) A*B/C
b) Q
+ A4 - 6.5
c)
F * H8 + J9
You should note that spaces
or
blanks are not significant in arithmetical expressions
of
this type. For example, the expression in a) could have been written
A*B/C
or A*B / C or
A * B/C
This allows you
to
space out the symbols so that they can be easily read.
However, it is important to note the order in which expressions such as c) are
evaluated.
In BASIC, mathematical operations always follow a hierarchical order. Exponentiaton
has the highest priority, multiplication and division are next, and finally. addition and
subtraction have the lowest.
Therefore, expression c) above, is interpreted
as
"the
product
of
F times H8 is added
to
J9."
However, if you wanted to add H8 and J9 together before multiplying their
sum times F, you would have to place parentheses around H8
+ J9:
F * (H8 + J9)
In this case the addition is performed first because the operation inside the parentheses
is
carried out before the outside multiplication.
Multiplication and division have equal priority and will
be
performed from left
to
right. The same is true
of
addition and subtraction.
If
you look at line 40 again
40 S = P *
(1
+ R * T)
it should be clear that the parentheses are needed. However, inside the parentheses,
the expression:
1
+ R * T
is
calculated correctly because the multiplication has higher priority than addition.
Experiment
#2
Compound Interest
If
you invest P dollars at an annual interest rate r, compounded k times a year, then
after
t years, your investment will have grown to the amount
S:
S =
PO
+ r/k)tk
A program will be written to calculate the value
of
S, for the following values
of
the
other variables:
Pis
$ 5,000
r is 12% per year
tis
5 years
k is 4 (i.e. interest is compounded 4 times a year.)
33