1.11 Using Logical Expressions

 <  Day Day Up  >  

You want to evaluate an expression to see whether it is true.


Technique

Use a combination of logical operators on each relational expression you want to test. Logical operations consist of the logical and operator ( && ), the logical or operator ( ), and the logical exclusive-or operator ( ^ ). The following example demonstrates the logical operators as applied to Boolean values:

 
 static void Main(string[] args) {     Console.WriteLine( "true==true: {0}", (true==true).ToString() );     Console.WriteLine( "true==false: {0}", (true==false).ToString() );     Console.WriteLine( "false==false: {0}", (false==false).ToString() );     Console.WriteLine( "truetrue: {0}", (truetrue).ToString() );     Console.WriteLine( "truefalse: {0}", (truefalse).ToString() );     Console.WriteLine( "falsefalse: {0}", (falsefalse).ToString() );     Console.WriteLine( "true^true: {0}", (true^true).ToString() );     Console.WriteLine( "true^false: {0}", (true^false).ToString() );     Console.WriteLine( "false^false: {0}", (false^false).ToString() ); } 

Comments

Logical operations test the results of two or more relational expressions. In the last recipe, you used relational operators to test the relationship of two values or objects with each other. This evaluation results in a Boolean value, true or false , which you can then act upon in some manner. One action is to compare the results from two or more of these relational expressions by using logical operations.

Logical operations belong to a larger family of operators known as bitwise operators. The term bitwise applies because these operators use the actual bits of a value to determine the result. For instance, a Boolean value of true is a single bit (at least in theory), which is "turned on" or equals 1. The Boolean value false is "turned off" or set to 0. By using this information, you can generate a table to see the result based on the logical or bitwise operator used on these two values, as shown in Table 1.2.

Table 1.2. Using Logical Operations

Operand 1

Operand 2

Operator

Result

Rule

True

True

&&

True

Both operands must be true.

True

False

&&

False

 

False

True

&&

False

 

False

False

&&

false

 

True

True

True

At least one operand must be true.

True

False

True

 

False

True

True

 

False

False

False

 

True

True

^

False

At least one operand is true, but not both.

True

False

^

True

 

False

True

^

True

 

False

False

^

False

 

The logical and, and the logical or operators are also known as short-circuit bitwise operators. If you are using a && operator, it makes sense that if the first operand is false, there is no need to test the second operand because you know that the logical statement will still evaluate to false. Furthermore, when using the operator, it makes no sense to test the second operand if the first is already true because that would automatically make the statement true. The exclusive-or operator cannot use any type of short-circuit evaluation because you must test both operands regardless of the value of the first. This difference is why you see two characters for && and but only one character for ^ . Furthermore, if you want to evaluate both operands and not short-circuit, each operator has a single bitwise operator implementation. However, you generally use these operators for bitwise manipulations of data.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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