Code

Boolean Logical Operators

Logical operators, also called Boolean operators, accept two bool values and return a Boolean result (true or false).

Boolean Logical Operators in C#

Operator Description Examples
&& Conditional AND Condition becomes true if both operands are true. Otherwise, return false. (a && a)
|| Conditional OR Condition becomes true if at least one of your operands is true. Otherwise, return false. (a || b)
! Logical NOT (Negation) Reverse the value of your Boolean variable, meaning If the condition is true then the operator will make it false and vice versa. !(a && b)
^ Logical XOR Returns true if one of the two operands has a true value. It also returns true if the operands have different values. (a ^ b)
Possible combinations for && (Logical AND) operator:
Console.WriteLine($"false && false: {false && false}");
Console.WriteLine($"false && true: {false && true}");
Console.WriteLine($"true && false: {true && false}");
Console.WriteLine($"true && true: {true && true}");
OUTPUT

false && false: False
false && true: False
true && false: False
true && true: True

Possible combinations for || (Logical OR) operator:
Console.WriteLine($"false || false: {false || false}");
Console.WriteLine($"false || true: {false || true}");
Console.WriteLine($"true || false: {true || false}");
Console.WriteLine($"true || true: {true || true}");
OUTPUT

false || false: False
false || true: True
true || false: True
true || true: True

Possible combinations for ! (Logical NOT) operator:
Console.WriteLine($"!false: {!false}");
Console.WriteLine($"!true: {!true}");
OUTPUT

!false: True
!true: False

Possible combinations for ^ (Exclusive OR) operator:
Console.WriteLine($"false ^ false: {false ^ false}");
Console.WriteLine($"false ^ true: {false ^ true}");
Console.WriteLine($"true ^ false: {true ^ false}");
Console.WriteLine($"true ^ true: {true ^ true}");
OUTPUT

false ^ false: False
false ^ true: True
true ^ false: True
true ^ true: False

Run Demo

short-circuits

Tagged in :

Add Comment

Click here to post a comment

Subscribe

Most Popular

Popular Video