5: BASIC Stamp Command Reference – IF…THEN
BASIC Stamp Programming Manual 2.0c • www.parallaxinc.com • Page 153
IF NOT x = 100 then NoCent
GOSUB Centennial ' IF x = 100 THEN GOSUB Centennial.
NoCent: ' Program continues.
Internally, the BASIC Stamp defines “false” as 0 and “true” as any value
other than 0. Consider the following instructions:
Flag VAR BIT
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. So it sees the NOT of that as
%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.
INTERNAL REPRESENTATION OF
BOOLEAN VALUES (TRUE VS. FALSE).
A
VOIDING ERRORS WITH BOOLEAN
RESULTS
.