Search

Friday, December 14, 2018

Relational and Logical Operators

In the terms relational operator and logical operator,relation all 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 shown here:


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 Boolean value

In Java, 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 and to type char. However, values of type Boolean 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 Java.
For the logical operators,the operands must be of type Boolean, and the result of a logical operation is of type Boolean. 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. 
class RelLogOps 
public static void main(String args[])
 { 
int i,j;
 Boolean b1, b2;
i = 10; 
j = 11; 
if(i < j)
 System.out.println("i < j"); 
if(i <= j) 
System.out.println("i <= j"); 
if(i != j) 
System.out.println("i != j"); if(i == j) 
System.out.println("this won't execute");
 if(i >= j)
 System.out.println("this won't execute");
 if(i > j)
 System.out.println("this won't execute");
b1 = true; 
b2 = false;
 if(b1 & b2) 
System.out.println("this won't execute"); 
if(!(b1 & b2))
 System.out.println("!(b1 & b2) is true");
 if(b1 | b2)
 System.out.println("b1 | b2 is true");
 if(b1 ^ b2) System.out.println("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

0 comments:

Post a Comment