In the terms relational operator and logical operator, relational refers to the relationships that values can have with one another, and logical refers to the ways in which true and false values can be connected together. Since the relational operators produce true or false results, they often work with the logical operators. For this reason they will be discussed together here.
The relational operators are as follows:
Operator | Meaning |
---|---|
= = | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
The logical operators are shown next:
Operator | Meaning |
---|---|
& | AND |
| | OR |
^ | XOR (exclusive OR) |
|| | Short-circuit OR |
&& | Short-circuit AND |
! | NOT |
The outcome of the relational and logical operators is a bool value.
In C#, all objects can be compared for equality or inequality using = = and !=. However, the comparison operators, <, >, <=, or >=, can be applied only to those types that support an ordering relationship. Therefore, all of the relational operators can be applied to all numeric types. However, values of type bool can only be compared for equality or inequality, since the true and false values are not ordered. For example, true > false has no meaning in C#.
For the logical operators, the operands must be of type bool, and the result of a logical operation is of type bool. The logical operators, &, |, ^, and !, support the basic logical operations AND, OR, XOR, and NOT, according to the following truth table:
p | q | p&q | p|q | p^q | !p |
---|---|---|---|---|---|
False | False | False | False | False | True |
True | False | False | True | True | False |
False | True | False | True | True | True |
True | True | True | True | False | False |
As the table shows, the outcome of an exclusive OR operation is true when exactly one and only one operand is true.
Here is a program that demonstrates several of the relational and logical operators:
// Demonstrate the relational and logical operators, using System; class RelLogOps { public static void Main() { int i, j; bool b1, b2; i = 10; j = 11; if(i < j) Console.WriteLine("i < j"); if(i <= j) Console.WriteLine("i <= j"); if(i != j) Console.WriteLine("i != j"); if(i == j) Console.WriteLine("this won't execute"); if(i >= j) Console.WriteLine("this won't execute"); if(i > j) Console.WriteLine("this won't execute"); b1 = true; b2 = false; if(b1 & b2) Console.WriteLine("this won't execute"); if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true"); if(b1 b2) Console.WriteLine("b1 | b2 is true"); if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true"); } }
The output from the program is shown here:
The logical operators provided by C# perform the most commonly used logical operations. However, there are several other operations defined by the rules for formal logic. These other logical operations can be constructed using the logical operators supported by C#. Thus, C# supplies a set of logical operators sufficient to construct any other logical operation. For example, another logical operation is implication. Implication is a binary operation in which the outcome is false only when the left operand is true and the right operand is false. (The implication operation reflects the idea that true cannot imply false.) Thus, the truth table for the implication operator is shown here:
p | q | p implies q |
---|---|---|
True | True | True |
True | False | False |
False | False | True |
False | True | True |
The implication operation can be constructed using a combination of the ! and the I operator, as shown here:
!p | q
The following program demonstrates this implementation:
// Create an implication operator in C#. using System; class Implication { public static void Main() { bool p=false, q=false; int i, j; for(i =0; i < 2; i++) { for(j = 0; j < 2; j++) { if (i==0) p = true; if (i==1) p = false; if (j==0) q = true; if (j==1) q = false; Console .WriteLine ("p is " + p + '' q is " + q); if(!p | q) Console.WriteLine(p + implies " + q + " is " + true); Console.WriteLine(); } } } }
The output is shown here:
p is True, q is True True implies True is True p is True, q is False p is False, q is True False implies True is True p is False, q is False False implies False is True
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
ReplyDeleteSelenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
ReplyDeletepython Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Training in Bangalore | Best AWS Amazon Web Services…
Amazon Web Services (AWS) Training in Pune India
AWS Training | AWS Training and Certification | AWS online training
AWS Training in Bangalore cost| Aws training in Bangalore with placements
I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
ReplyDeletepython Course in Pune
python Course institute in Chennai
python Training institute in Bangalore
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
ReplyDeleteAWS Training in pune
AWS Online Training
Thanks For Sharing The Information The information Shared Is Very valuable Please keep updating us Time Just Went On reading The article Python Online Course AWS Online Course Devops Online Course DataScience Online Course
ReplyDeleteThe reference tables are helpful.
ReplyDelete