I
NTERNAL REPRESENTATION OF
B
(T
OOLEAN VALUES
RUE VS
A
B
VOIDING ERRORS WITH
OOLEAN
.
RESULTS
5: BASIC Stamp Command Reference – IF...THEN
IF NOT x = 100 then NoCent
GOSUB Centennial
NoCent:
Internally, the BASIC Stamp defines "false" as 0 and "true" as any value
. F
).
ALSE
other than 0. Consider the following instructions:
Flag
VAR
Flag = 1
IF Flag THEN IsTrue
DEBUG "false"
END
IsTrue:
DEBUG "true"
END
Since Flag is 1, IF...THEN would evaluate it as true and print the message
"true" on the screen. Suppose you changed the IF...THEN command to
read "IF NOT Flag THEN IsTrue." That would also evaluate as true.
Whoa! Isn't NOT 1 the same thing as 0? No, at least not in the 16-bit world
of the BASIC Stamp.
Internally, the BASIC Stamp sees a bit variable containing 1 as the 16-bit
number
%0000000000000001.
%1111111111111110. Since any non-zero number is regarded as true, NOT
1 is true. Strange but true.
The easiest way to avoid the kinds of problems this might cause is to
always use a conditional operator with IF...THEN. Change the example
above to read IF Flag = 1 THEN IsTrue. The result of the comparison will
follow IF...THEN rules. Also, the logical operators will work as they
should; IF NOT Flag = 1 THEN IsTrue will correctly evaluate to false
when Flag contains 1.
This also means that you should only use the "named" conditional logic
operators NOT, AND, OR, and XOR with IF...THEN. The conditional logic
operators format their results correctly for IF...THEN instructions. The
other logical operators, represented by symbols ~ & | and ^ do not; they
are binary logic operators.
BIT
So
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 153
' IF x = 100 THEN GOSUB Centennial.
' Program continues.
it
sees
the
NOT
of
that
as
Need help?
Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?