In the C#, The conditional logical AND operator &&, also known as the "short-circuiting" logical AND operator, ||, Logical or, Returns true if one of the statements is true.
In the c# programming language, the Logical Operators will always work with Boolean expressions (true or false) and return Boolean values.
The operands in logical operators must always contain only Boolean (true or false) values. Otherwise, Logical Operators will throw an exception.
Operator | Name | Description | Example (a = true, b = false) |
&& | Logical AND | returns true if both operands are non-zero. | a && b (false) |
|| | Logical OR | returns true if any one operand becomes a non-zero. | a || b (true) |
! | Logical NOT | It will return the reverse of a logical state that means if both operands are non-zero, it will return false. | !(a && b) (true) |
If we use Logical AND, OR operators in c# language, those will return the result as shown as given below for different inputs.
Operand1 | Operand2 | AND | OR |
true | true | true | true |
false | true | false | true |
true | false | false | true |
false | false | false | false |
If you observe the given above table example, if anyone's operand value becomes false, then the logical AND operator will return false. The logical OR operator will return true if any one operand value becomes true.
If you use the Logical NOT operator in our c# applications, it will return the results as shown given below for different inputs.
Operand | NOT |
true | false |
false | true |
If we observe the given above table example, the Logical NOT operator will always return the reverse value of the operand. If the operand value is true, then the Logical NOT operator will return false and vice versa.
C# Logical Operators Example
using System;
namespace FindAndSolve
{
public class Program
{
public static void Main(string[] args)
{
int a = 8, b = 4;
bool ab = true, result;
// AND operator
result = (a <= b) && (a > 5);
Console.WriteLine("AND Operator: " + result);
// OR operator
result = (a >= b || (a < 5));
Console.WriteLine("OR Operator: " + result);
//NOT operator
result = !ab;
Console.WriteLine("NOT Operator: " + result);
}
}
}
Output
AND Operator: False
OR Operator: True
NOT Operator: False