34,333
edits
Changes
→Logical Operators
<br>
== Boolean Logical Operators == Another set of operators which return boolean ''true'' and ''false'' values are the C# ''boolean logical operators''. These operators both return boolean results and take boolean values as operands. The key operators are NOT (!), AND (&&), OR (||) and XOR (^). The NOT (!) operator simply inverts the current value of a boolean variable. For example, if a variable named ''flag'' is currently ''true'', prefixing the variable with a '!' character will invert the value to be ''false'': <pre>bool flag = true; //variable is truebool secondFlag; secondFlag = !flag; // secondFlag set to false</pre> The OR (||) operator returns ''true'' if one of its two operands evaluates to ''true'', otherwise it returns false. For example, the following example evaluates to true because at least one of the expressions either side of the OR operator is true: <pre>if ((10 < 20) || (20 < 10)) System.Console.WriteLine("Expression is true");<pre> The AND (&&) operator returns ''true'' only if both operands evaluate to be true. The following example will return ''false'' because only one of the two operand expressions evaluates to'' true'': <pre>if ((10 < 20) && (20 < 10)) System.Console.WriteLine("Expression is true");</pre> The XOR (^) operator returns ''true'' if one and only one of the two operands evaluates to true. For example, the following example will return ''true'' since only one operator evaluates to be true: <pre>if ((10 < 20) ^ (20 < 10)) System.Console.WriteLine("Expression is true");</pre> If both operands evaluated to be true or both were false the expression with return false. == The Ternary Operator ==