Logical operators in computer programming are operators used to control program flow based on the values of certain conditions. The operands can be considered as a conditional value resulting in a true or false. Although the operators are called logical operators yet their use is not restricted to the Boolean values but they can be used with all the types. The primary function of the logical operators is in the transformation of the relational statements into conditional values. In C# logical operators are used to performing logical operations on two or more operands.
These logical operations include logical AND, logical OR, Logical NOT. The logical operators can be used as logical conditional operators as well as relational conditional operators and the use of operand value in terms of their existence as physical or Boolean value is dependent on the use of the logical operator in the operation. The logical operators are basic translations of the logical GATE operations and follow the exact logic of the logical GATE counterparts.
Below are explained in detail the top four logical operators in c #:
The logical AND operator evaluate to true if the value of both the operands is true i.e. the value of the logical AND operation is only equal to true if and only if the operands used in the operation evaluate to true themselves. The logical operation of AND is denoted by the use of two ampersand &&.
Name | Description | Syntax | Symbol |
Logical AND | The logical operation yields true if and only if the value of the operand in non zero | a && b | && |
Truth Table:
|
||||||||||||||||||||
A | B
|
OUTPUT |
||||||||||||||||||
TRUE | TRUE | TRUE | ||||||||||||||||||
TRUE | FALSE | FALSE | ||||||||||||||||||
FALSE | TRUE | FALSE | ||||||||||||||||||
FALSE | FALSE | FALSE |
Logical OR | ||
A | B | OUTPUT |
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
Name | Description | Syntax | Symbol |
Logical OR | The logical operation yields true if the value of any of its operand in non zero. | a || b | || |
Logical OR | ||
A | B | OUTPUT |
TRUE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
The physical existence of the logical NOT is based on the negation principle. The logical NOT operator as the name suggests is used in the negation of the primary operand to its logically opposite value.
Name | Description | Syntax | Symbol |
Logical NOT | The logical operation yields true if the value of the operand is zero or False. | !a | ! |
The logical XOR condition evaluates to true if and only if the value of both the operands in the operation are unequal. This is represented by the symbol ^. This is widely used in conditions where the segregation is required based on the equality of the operands.
Name | Description | Syntax | Symbol |
Logical Exclusive OR | The logical operation yields true if the value of both of its operands is unequal. | a^ b | ^ |
Truth Table:
|
||||||||||||||||||||
A | B | OUTPUT | ||||||||||||||||||
TRUE | TRUE | FALSE | ||||||||||||||||||
TRUE | FALSE | TRUE | ||||||||||||||||||
FALSE | TRUE | TRUE | ||||||||||||||||||
FALSE | FALSE | FALSE |
Examples of Logical AND
Following are the examples of logical AND operator in C#.using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int X = 11, Y = 10; bool logicalAND; // AND operator logicalAND = (X <= Y) && (X > 10); Console.WriteLine(" Result of AND Operation : " + logicalAND); Console.WriteLine("Press enter to Exit"); Console.ReadLine(); } } }
Example #1
Code:Output:
The true value of AND operation comes if we change the value of y.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int x = 11, y = 20; bool logicalAND; logicalAND = (x <= y) && (x > 10); Console.WriteLine(" Result of AND Operation : " + logicalAND); Console.WriteLine("Press enter to Exit.."); Console.ReadLine(); } } }
Example #2
Code:Output:
Examples of Logical OR
Following are the examples of logical OR operator in C#.using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int X = 11, Y = 20; bool logicalOR; // AND operator logicalOR = (X >= Y) || (X < 8); Console.WriteLine(" Result of OR Operation : " + logicalOR); Console.WriteLine("Press enter to Exit"); Console.ReadLine(); } } }
Example #1
Code:Output:
This will evaluate to False as both the logical operands evaluate to a value of false. To demonstrate the true occurrence of the OR operator lets change the value of X to 21 i.e. greater than Y.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int X = 21, Y = 20; bool logicalOR; // AND operator logicalOR = (X >= Y) || (X < 8); Console.WriteLine(" Result of OR Operation : " + logicalOR); Console.WriteLine("Press enter to Exit"); Console.ReadLine(); } } }
Example #2
Code:Output:
Example of Logical NOT
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { bool a = true, logicalNOT; logicalNOT = !a; Console.WriteLine(" Result of NOT Operation : " + logicalNOT); Console.WriteLine("Press enter to Exit"); Console.ReadLine(); } } }
Following are the examples of logical NOT operators in C#.
Code:Output:
Example of Logical Exclusive OR
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int X = 21, Y = 22; bool logicalXOR; logicalXOR = (X > Y) ^ (X < 22); Console.WriteLine(" Result of XOR Operation : " + logicalXOR); Console.WriteLine("Press enter to Exit"); Console.ReadLine(); } } }
Following are the examples of the logical exclusive OR operator in C#.
Code:Output:
This will yield true as the value of X > Y operand is false and X<22 operand is true. Conclusion From the above examples, we have become aware of the various logical operators in C#. The primary use of the logical operators is found in decision flow diagrams where they are used in conditional decision making based on the status of the operands. AND, OR, NOT operators are traditional logical operators used in the condition evaluation whereas XOR is rather a modern operator. The term logical operator comes due to the fact that the output of all the operations involving logical operators is a bool value i.e either true or false.
The above is the detailed content of Logical Operators in C#. For more information, please follow other related articles on the PHP Chinese website!