Specifications

BASIC Stamp II
Parallax, Inc. • BASIC Stamp Programming Manual 1.8 • Page 233
2
The BS2 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 BS2 goes through a sequence like this:
12 + 3 = 5
5 * 2 = 10
10 / 4 = 2
the answer is 2
Note that because the BS2 performs integer math (whole numbers only)
that 10 / 4 results in 2, not 2.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 multipli-
cation and division be done before addition. So the result would be:
3 * 2 = 6
6 / 4 = 1
12 + 1 = 13
the answer is 13
Once again, because of integer math, the fractional portion of 6 / 4 is
dropped, so we get 1 instead of 1.5.
Given the potential for misinterpretation, we must use parentheses to
make our mathematical intentions clear to the BS2 (not to mention
ourselves and other programmers who may look at our program). With
parentheses. Enclosing a math operation in parentheses gives it prior-
ity over other operations. For example, in the expression 1+(3*4), the
3*4 would be computed first, then added to 1.
To make the BS2 compute the previous expression in the conventional
BASIC way, you would write it as 12 + (3*2/4). Within the parenthe-
ses, the BS2 works from left to right. If you wanted to be even more
specific, you could write 12 + ((3*2)/4). When there are parentheses
within parentheses, the BS2 works from the innermost parentheses
outward. Parentheses placed within parentheses are said to be nested.
The BS2 lets you nest parentheses up to eight levels deep.