Understanding Boolean Logic

   

Boolean logic is a special type of arithmetic/comparison. Boolean logic is used to evaluate expressions down to either true or false. This may be a new concept to you, but don't worry; it's not difficult to understand. Boolean logic is performed using a logical operator. Consider the following sentence :

If black is a color and wood comes from trees then print "ice cream."

At first glance, it might seem that this is nonsensical . However, C# could make sense of this statement using Boolean logic. First, notice that three expressions are actually being evaluated within this single sentence. I've added parentheses in the following sentence to clarify two of the expressions. If (black is a color) and (wood comes from trees) then print "ice cream."

Boolean logic evaluates every expression to either true or false. Therefore, substituting true or false for each of these expressions yields the following:

if (true) And (true) then print "ice cream."

Now, for the sake of clarity, here is the same sentence with parentheses placed around the final expression to be evaluated:

If (True And True) then print "ice cream."

This is the point where the logical operators come into play. The And (&&) operator returns true if the expressions on each side of the And (&&) operator are true (see Table 13.2 for a complete list of logical operators). In the sentence we're considering, the expressions on both sides of the And (&&) operator are true, so the expression evaluates to true. Replacing the expression with true yields:

If True then print "ice cream."

This would result in the words "ice cream" being printed. If the expression had evaluated to false, nothing would be printed. As you'll see in the next hour , the decision constructs always fully evaluate their expressions to either true or false, and statements execute according to the results.

Table 13.2. Logical (Boolean) Operators
Operator Description
And (&&) Evaluates to true when the expressions on both sides are true.
Not (!) Evaluates to true when its expression evaluates to false; otherwise , it returns false (the true/false value of the expression is negated, or reversed ).
Or () Evaluates to true if an expression on either side evaluates to true.
Xor (^) Evaluates to true if one, and only one, expression on either side evaluates to true.

Each of these is discussed in the following sections.

Using the And (&&) Operator

The And (&&) operator is used to perform a logical conjunction. If the expressions on both sides of the And (&&) operator evaluate to true, the And (&&) operation evaluates to true. If either expression is false, the And (&&) operation evaluates to false, as illustrated in the following examples:

 Debug.WriteLine(true && true);            // Prints true Debug.WriteLine(true && false);           // Prints false Debug.WriteLine(false && true);           // Prints false Debug.WriteLine(false && false);          // Prints false Debug.WriteLine((32 > 4) && (6 == 6));    // Prints true 

Using the Not(!) Operator

The Not(!) operator performs a logical negation. That is, it returns the opposite of the expression. Consider the following examples:

 Debug.WriteLine(! (true));      // Prints false Debug.WriteLine(! (false));     // Prints true Debug.WriteLine(! (5 == 5));    // Prints false Debug.WriteLine(!(4 < 2));      // Prints true 

The first two statements are easy enough; the opposite of true is false and vice versa. For the third statement, remember that C#'s operator precedence dictates that arithmetic operators are evaluated first (even if no parentheses are used), so the first step of the evaluation would look like this:

 Debug.WriteLine( ! (true)); 

The opposite of true is false, of course, so C# prints false.

The fourth statement would evaluate to:

 Debug.WriteLine( !(false)); 

This happens because 4 is not less than 2, which is the expression C# evaluates first. Because the opposite of false is true, this statement would print true.

Using the Or () Operator

The Or() operator is used to perform a logical disjunction. If the expression to the left or right of the Or() operator evaluates to true, the Or() operation evaluates to true. The following are examples using Or() operations, and their results:

 Debug.WriteLine(true  true);          // Prints true Debug.WriteLine(true  false);         // Prints true Debug.WriteLine(false  true);         // Prints true Debug.WriteLine(false  false);        // Prints false Debug.WriteLine((32 < 4)  (6 == 6));  // Prints true 

Using the Xor (^) Operator

The Xor(^) operator performs a nifty little function. I personally haven't had to use it much, but it's great for those times when its functionality is required. If one ”and only one ”of the expressions on either side of the Xor(^) operator is true, the Xor(^) operation evaluates to true. Take a close look at the following statement examples to see how this works:

 Debug.WriteLine(true ^ true);           // Prints false Debug.WriteLine(true ^ false);          // Prints true Debug.WriteLine(false ^ true);          // Prints true Debug.WriteLine(false ^ false);         // Prints false Debug.WriteLine((32 < 4) ^ (6 == 6));   // Prints true 

   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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