Logical Operators in C#
Notes:
Logical Operators in C# Programming Language:
&&: Logical And Operator:
If both LHS and RHS operands are true then the result will be true, in all other cases the result will be false
||: Logical Or Operator:
If both LHS and RHS operands are false then the result will be false, in all other cases the result will be true
!: Logical Not Operator:
If the given operand is true, then the result will be false. Vice versa
Example Code:
using System;
namespace LogicalOperatorsDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine((3 < 4) && (4 < 5)); // True
Console.WriteLine((3 < 4) && (4 > 5)); // False
Console.WriteLine((3 > 4) && (4 < 5)); // False
Console.WriteLine((3 > 4) && (4 > 5)); // False
Console.WriteLine();
Console.WriteLine((3 < 4) || (4 < 5)); // True
Console.WriteLine((3 < 4) || (4 > 5)); // True
Console.WriteLine((3 > 4) || (4 < 5)); // True
Console.WriteLine((3 > 4) || (4 > 5)); // False
Console.WriteLine();
Console.WriteLine(!(3 < 4)); // False
Console.WriteLine(!(4 > 5)); // True
Console.ReadKey();
}
}
}