Logical Operators

Logical operators are a base for expressions and conditional statements. You can view all of the Blitz3D logical operators in Table 2.5. It lists all of the conditions that make the logical operators true and false.

Table 2.5. Logical Operator Truth Table
PQP AND QP OR QNOT P
00001
01011
11110
10010

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. On the next page is an example of the AND operator.

;demo02-09.bb - Shows use of the And operator

;find out how old the user is
age = Input$("How old are you? ")
;find out if the user lives in america
location = Input$("Do you live in America? (1 For yes, 2 For no) ")

;Write out the proper string depending on the user's age and location
If age >= 18 And location = 1 Then
     Print "Congrats, you are eligible to vote!"
Else
     Print "Sorry, you can't
EndIf

;Wait five seconds
Delay 5000

The output is shown in Figure 2.11.

Figure 2.11. The demo02-09.bb program.


The NOT Operator

The NOT operator is a little bit different from 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 zero and true is one, the only value NOT will return is one or zero. If you write

Not 0

your answer will be one, and conversely if you write

Not 1

your answer will be zero.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.232.189