Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

The If Statement Page 2 3 4 5 6 7 8 9
Arithmetic Operations Page 2 3 4 5 6 Boolean Operators and Nested If Statements Page 2 3 4 5 6 7
Events and Sequential Processing Page 2 3 4 5 More Examples Page 2 3 4 5 6 7 8 9 10 11 12
Datatypes and Conversions Page 2 3 4 5 6 7 Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10
Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exercises Page 2 3 4 5 6 7 8
Chapter 4- Selection Statements Review Questions
Introduction

4-5: Boolean Operators

The Operators

Table 4-3 shows some of the boolean operators used in Visual Basic. In the table op1 and op2 refer to the two operands of a binary boolean operator and the result of the operation for the various possible values of these operands is shown.

Table 4-3: A partial list of Boolean Operators.

Operator Description

AND The result of op1 AND op2 for the given operand values is:
Op1 Op2 Result
True True True
True False False
False True False
False False False

OR The result of op1 OR op2 for the given operand values is:
Op1 Op2 Result
True True True
True False True
False True True
False False False

XOR The result of op1 XOR op2 for the given operand values is:
Op1 Op2 Result
True True False
True False True
False True True
False False False

NOT (unary) The result of Not op1 is:
Op1 Result
True False
False True

The AND, OR, and NOT operators correspond to the intuitive understanding we have of these words. You might often say something like "If the weather is fine and its windy I'll fly my kite!" (well, if you were a kite enthusiast you might say this!) This means that both "weather is fine" and "is windy" have to be true for you to fly your kite. If it is windy but raining you will not fly your kite. If it the weather is fine but its not windy you'll not fly your kite. And if it is raining and its not windy you'll not fly your kite.

The OR operator is also similar to everyday usage. You might say "I'll go with you if Jim or Mary also come." This generally means that if "Jim comes" is true, you'll also go. If "Mary comes" is true, you'll also go. And if both "Jim comes" and "Mary comes" is true, you'll also go. Only in the case that "Jim comes" is false and "Mary comes" is false will you not go too.

The XOR operator is a little beyond normal everyday usage. Consider the case that perhaps you're going wherever (in the previous example) in a small car. Then your statement "I'll go with you if Jim or Mary also come." is likely to mean "if only one of them come". That is, unlike the OR operator, the XOR operator produces a False result when the two operands are both True.