Section 7.4. Logical Operators Within Conditionals

   

7.4 Logical Operators Within Conditionals

If statements (discussed in Chapter 6) test whether a condition is true. Often you will want to test whether two conditions are both true, or only one is true, or neither is true. VB.NET provides a set of logical operators for this, as shown in Table 7-2. This table assumes two variables , x and y, in which x has the value 5, and y has the value 7.

Table 7-2. Logical operators

Operator

Given this statement

The expression evaluates to

Logic

And

 x = 3 And y = 7 

False

Both must be true to evaluate true.

Or

 x = 3 Or y = 7 

True

Either or both must be true to evaluate true.

XOr

 X = 5 XOr y = 7 

False

True only if one (and only one) statement is true.

Not

Not x = 3

True

Expression must be false to evaluate true.

The And operator tests whether two statements are both True. The first line in Table 7-2 includes an example that illustrates the use of the And operator:

 x = 3 And y = 7 

The entire expression evaluates false because one side ( x = 3 ) is false. (Remember that x=5 and y=7.)

With the Or operator, either or both sides must be true; the expression is false only if both sides are false. So, in the case of the example in Table 7-2:

 x = 3 Or y = 7 

the entire expression evaluates true because one side ( y = 7 ) is true.

The XOr logical operator (which stands for eXtreme Or) is used to test if one (and only one) of the two statements is correct. Thus, the example from Table 7-2:

 X = 5 XOr y = 7 

evaluates false because both statements are true. (The XOr statement is false if both statements are true, or if both statements are false; it is true only if one, and only one, statement is true.)

With the Not operator, the statement is true if the expression is false, and vice versa. So, in the accompanying example:

 Not x = 3 

the entire expression is true because the tested expression ( x = 3 ) is false. (The logic is: "it is true that it is not true that x is equal to 3.")

All of these examples appear in context in Example 7-4.

Example 7-4. The logical operators
 Option Strict On Imports System Module Module1    Sub Main( )       Dim x As Integer = 5       Dim y As Integer = 7       Dim andValue As Boolean       Dim orValue As Boolean       Dim xorValue As Boolean       Dim notValue As Boolean       andValue = x = 3 And y = 7       orValue = x = 3 Or y = 7       xorValue = x = 3 Xor y = 7       notValue = Not x = 3       Console.WriteLine("x = 3 And y = 7. {0}", andValue)       Console.WriteLine("x = 3 Or y = 7. {0}", orValue)       Console.WriteLine("x = 3 Xor y = 7. {0}", xorValue)       Console.WriteLine("Not x = 3. {0}", notValue)    End Sub 'Main  End Module 
  Output:  x = 3 And y = 7. False x = 3 Or y = 7. True x = 3 Xor y = 7. True Not x = 3. True 
   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net