Boolean Type
A
Boolean value can be either
true or
false.
The name "boolean" comes from George Boole, a 19th century mathematician who invented the algebraic system for these
true and
false values.
Boolean values are used to make decisions.
For instance, an
if statement uses a boolean value to decide whether to run one block of statements or another.
A
while statement uses a boolean value at the end of each loop to decide whether to loop again.
The values
true and
false can be used directly, as in the following example.
dim x as Boolean
x = true
x = false
The
not operator will change a
true value to
false, or a
false value to
true.
dim x = not false
dim y = not x
The
and operator forms a new boolean value by comparing two input boolean values.
The result is
true only if both of the inputs are
true.
dim a = true
dim b = false
dim c = true
dim d = a and b
dim e = a and c
The
or operator works like the
and operator, but the result is true if either (or both) of the inputs are
true.
dim a = true
dim b = false
dim c = false
dim d = a or b
dim e = b or c
It is often useful to write a function that inspects its parameters and returns a boolean based on some criteria.
In the following example, the
IsMedicalProfession function returns
true if the input is "doctor" or "nurse", but
false for "programmer".
function IsMedicalProfession(profession as String) as Boolean
return profession = "doctor" or profession = "nurse"
end function