4: BASIC Stamp Architecture – Order of Operations
You may recall that the order in which you do a series of additions and
subtractions doesn't affect the result. The expression 12+7-3+22 works out
the same as 22-3+12+7. However, when multiplication or division are
involved, it's a different story; 12+3*2/4 is not the same as 2*12/4+3. In
fact, you may have the urge to put parentheses around portions of those
equations to clear things up.
The BASIC Stamp solves math problems in the order they are written;
from left to right. The result of each operation is fed into the next
operation. So to compute 12+3*2/4, the BASIC Stamp goes through a
sequence like this:
Note that since the BASIC Stamp performs integer math (whole numbers
only) 30 / 4 results in 7, not 7.5. We'll talk more about integers in the next
section.
Some other dialects of BASIC would compute that same expression based
on their precedence of operators, which requires that multiplication and
division be done before addition. So the result would be:
Once again, because of integer math, the fractional portion of 6 / 4 is
dropped, so we get 1 instead of 1.5.
1
The BS1 does not allow parenthesis in expressions. Unfortunately, all
expressions have to be written so that they evaluate as intended strictly
from left to right.
2
2
2
2
The BS2, BS2e, BS2sx and BS2p, however, allow parenthesis to be used to
e
p
sx
change the order of evaluation. Enclosing a math operation in parentheses
gives it priority over other operations. To make the BASIC Stamp compute
the previous expression in the conventional way, you would write it as 12
+ (3*2/4). Within the parentheses, the BASIC Stamp works from left to
12 + 3 = 15
15 * 2 = 30
30 / 4 = 7
3 * 2 = 6
6 / 4 = 1
12 + 1 = 13
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 61
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?