4: BASIC Stamp Architecture – Order of Operations
BASIC Stamp Programming Manual 2.0c
•
www.parallaxinc.com
•
Page 61
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:
12 + 3 = 15
15 * 2 = 30
30 / 4 = 7
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:
3 * 2 = 6
6 / 4 = 1
12 + 1 = 13
Once again, because of integer math, the fractional portion of 6 / 4 is
dropped, so we get 1 instead of 1.5.
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.
The BS2, BS2e, BS2sx and BS2p, however, allow parenthesis to be used to
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
1
2
e
2
sx
2
p
2
Summary of Contents for BASIC Stamp 2e
Page 1: ...BASIC Stamp Programming Manual Version 2 0c...
Page 34: ...Quick Start Guide Page 32 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 340: ...ASCII Chart Page 338 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 342: ...Reserved Words Page 340 BASIC Stamp Programming Manual 2 0b www parallaxinc com...
Page 346: ...Conversion Formatters Page 344 BASIC Stamp Programming Manual 2 0b www parallaxinc com...