Understanding of Logical Operators (Conditional) in C# (C Sharp)
five different logical operators in C#. They are &, |, ^, &&,
||. The last two operators && and || are called conditional logical
operators. First three operators (&, |, ^) mentioned above can take
part in both bitwise operations and Boolean operations. But &&
and || are used only to evaluate Boolean expressions. This article will
help you in understanding the purpose of conditional logical operators.
&& operator does the same job as & operator except that it doesnt involve in bitwise operations. Both share the same truth table. Similarly, || operator is equivalent to | operator excluding bitwise operations. When && and || does the same job as that of & and | respectively, what is the need for these two operators? How is it distinct from & and |? There is one major difference. Conditional Logical operators consume less time when compared to the bitwise operators. How is it possible when both do the same operation? This is discussed in detail in this article.
&& and || are also termed as short-circuit operators. They accept two Boolean expressions as operands. They evaluate the first operand and based on its result they decide whether to evaluate the other Boolean expression or not. But & and | operator evaluate both the operands at all times. Hence the earlier operators are better time consuming when compared to the later.
&& Operator: && operator returns true only if both the Boolean expressions evaluate to true. In all other cases, it returns false. Hence when you evaluate the first Boolean expression, if its value is false then even if second Boolean expression results in true, result of && operation will be false. Hence if the first operand returns false, there is no need to evaluate the second operand. This saves time and improves speed. This is what is done by && operator. Given below is the table illustrating whether both the expressions will be evaluated or only one expression, based on the operand values:
In the above table, Operand1 and Operand2 are two Boolean expressions. Operand1 will be evaluated first and only if it is true, then the second operand will be evaluated. Though Bitwise & operator gives the same result, it evaluates both the expressions in all cases.
|| Operator: Similar to && operator, || operator evaluates the second Boolean expression only based on the value of the first Boolean expression. || Operator returns true is any of the operand value is true. Hence if the first expression is evaluated to true, irrespective of the second expression value the result is going to be true. Therefore there is no need to evaluate the second expression if the first expression evaluates to true. Given below is the table illustrating all possible cases based on operands value:
Example for && and || Operator:
Given below is a simple example using these two operators :
Output of this code will be:
contains an even number greater than 50
| .NET 3.5 Framework New Features and Benefits | Overview of .NET Framework 3.5 Architecture | Overview of Unary Operators of C# (C Sharp) | Understanding of Checked and Unchecked Statement Type of C# (C Sharp) | Understanding of fixed Statement in C# (C Sharp) | Understanding of lock Statement in C# (C Sharp) | Understanding of Logical Operators (Bitwise, Boolean) in C# (C Sharp) | Understanding of Logical Operators (Conditional) in C# (C Sharp) |