Subscribe

RSS Feed (xml)

Relational and Logical Operators in C#

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:
i < j
i <= j
i != j
! (b1 & b2) is true
b1 | b2 is true
b1 ^ b2 is true   
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

0 comments:

Post a Comment

Archives

LocalsAdda.com-Variety In Web World

Fun Mail - Fun in the Mail