Original Author Paul Laughton, 2011
Page 54
De Re BASIC!
Logical Expression <lexp>
Logical expressions, or Boolean expressions, produce only two results: false or true. False is represented
in BASIC! by the numeric value of zero. Anything that is not zero is true. False = 0. True = not 0.
There are two types of logical expressions: Numeric logical expressions and string logical expressions.
Both types produce a numerically-represented values of true or false. Each type consists of one or more
variables or constants separated by binary logical operators, formally defined like this:
<slexp> := {<string variable>|<string constant>} <logical operator> {<string variable>|<string constant>}
<nlexp> := {<numeric variable>|<numeric constant>} <logical operator> {<numeric variable>|<numeric
constant>}
There is also the unique unary NOT (!) operator. NOT inverts the truth of a logical expression.
Logical Operators
Most of the logical operators are used for comparison. You can compare strings or numbers (<, =, etc.).
You can use the other Boolean operators (!, &, |) on numbers but not on strings.
This table shows all of the logical operators. They are listed by precedence with the highest precedence
first. All of these operators have lower precedence than any of the numeric operators or the one string
operator. Precedence may be modified by using parentheses.
Precedence Operator
Meaning
Operands
1
<
>
<=
>=
=
<>
Less Than
Greater Than
Less Than or Equal
Greater Than or Equal
Equal
Not Equal
Two <nlexp> or two <slexp>
2
!
Unary Not
One <nlexp> only
3
&
And
Two <nlexp> only
4
|
Or
Two <nlexp> only
Examples of Logical Expressions
1 < 2 (true)
3 <> 4 (true)
"a" < "bcd" (true)
1 & 0 (false)
!(1 & 0) (true)
Parentheses
Parentheses can be used to override operator precendence.
a = b * c + d
% the multiplication is done first
a = b * (c + d)
% the addition is done first