BASIC stamp manual v2.2
IF…THEN – BASIC Stamp Command Reference
Page 236 • BASIC Stamp Syntax and Reference Manual 2.2 • www.parallax.com
Internally, the BASIC Stamp defines “false” as 0 and “true” as any value
other than 0. Consider the following instructions:
flag VAR Bit
Setup:
flag = 1
Test:
IF flag THEN Is_True
DEBUG "False"
END
Is_True:
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 Is_True.” 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 is_True. The result of the comparison
will follow IF...THEN rules. Also, the logical operators will work as they
should; IF NOT Flag = 1 THEN is_True 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.
The remainder of this discussion only applies to the extended IF…THEN
syntax supported by PBASIC 2.5.
INTERNAL REPRESENTATION OF BOOLEAN
VALUES
(TRUE VS. FALSE).
A
VOIDING ERRORS WITH BOOLEAN
RESULTS
.
All
2
All
2