[ LiB ] |
Logical operators are a base for expressions and conditional statements. You can view all of the Blitz Basic logical operators in Table 2.5. It lists all of the conditions that make the logical operators true and false.
P | Q | P AND Q | P OR Q | NOT P |
---|---|---|---|---|
|
|
|
| 1 |
| 1 |
| 1 | 1 |
1 | 1 | 1 | 1 |
|
1 |
|
| 1 |
|
The AND operator is true only if both its parameters are true, the OR operator is true if one or more of its parameters are true, and the NOT operator is true only if its parameter is false. Here is an example of the AND operator.
;demo02-09.bb Shows use of the And operator age = Input$("How old are you? ") location = Input$("Do you live in America? (1 for yes, 2 for no) ") If age >= 18 And location = 1 Then Print "Congrats, you are eligible to vote!" Else Print "Sorry, you can't vote." EndIf WaitKey
The output is shown in Figure 2.11.
The NOT operator is a little bit different than the other two logical operators. Instead of two operands, it only takes one. And instead of returning a value based on the other two operands, it only returns the opposite of the operand it is working on.
Remember that because false is 0 and true is 1, the only value NOT will return is 1 or 0. If you write
Not 0
your answer will be 1, and if you write
Not 1
your answer will be 0.
[ LiB ] |