Scanner class Methods in Java

Java- Logical Operators

Logical Operators

Logical operators are used to combine one or more relational expression to evaluate their result.This results in formation of complex expression known as logical expression These operators can check two or more conditions and give the output in boolean value true or false.

These operators works same as logical gates of digital electronics.They can combine two or more inputs and give their result in one output.

Example: Lets understand the logical expression and operator with an example,

(5==7)  && (5<7), This is a logical expression ,which groups together two relational expressions. "&&" is a conditional AND operators. It will compare the result of both relational expressions. And output one boolean value.
Here, It will first check if 5 is equal to 7, if 5 is not equal to seven it will return false as result because for Logical  AND operators, If both expressions results true only then it will print true , if any of the expression results false the result will be false.

AND vs OR 

You know what AND means , it is same as in English. For example, you say X and Y are playing, which means two children are playing together. You will call it X OR Y ,if only one child is playing and you don't know whether it is X or Y.
Same in programming , AND is to check two conditions if they both are true and OR is to check it any of the condition is true.

Use or Pros of Logical Operators

  • Logical operators are very useful as they can check as many as conditions you want to evaluate simultaneously. 
  • They can save you from writing same code repeatedly  to check the relative conditions. 
  • They helps in reducing the lines of code.
  • Your program becomes less complex and easy to read.

Types of Logical Operators

There are six types of logical operators as follows,
Java Types of Logical Operators

1) Logical AND(&): Logical AND operator is used to evaluate the results of two or more expressions at same time.The result of Logical AND will be true if and only if both the conditions are true else it will return false. It means if exp1 and exp2 are true the output will be true, else if any of the expression is false the value will be false.
Example:      (3>7) &  (3!=7),here the first expression checks if 3 is greater than 7 , and second will check if 3 is not equal to 7. According to simple maths rule, we know first expression is false and second is true , but the & will return as false as it requires both the expressions to be true.

2) Logical OR (|): Logical OR operator is used to evaluate the results of two or more expressions at same time.The result of Logical OR will be false if and only if both the conditions are false else it will return true. It means if exp1 and exp2 are false the output will be false, else if any of the expression is true the value will be true.
Example:   We will use same example as it makes easy to understand the difference between  operators.
(3>7) |  (3!=7),here the first expression checks if 3 is greater than 7 , and second will check if 3 is not equal to 7.The first expression is false , but the second expression is true so the output will also be true as in Logical OR , to return false both the expression must be false.

3) Logical NOT(!): Logical NOT operator is a different operator as it can change the result of the operand or expression with whom it is used. It is unary operators, it requires only one operand or expression. It is used to inverse the output.If the output of expression or operand is true , it will return false and vice versa.
Example:      !(3==3), here first it checks for 3 is equals to 3, We know the output is true as  3 is equal to 3 , but the ! symbol here is used to negate the result . It will give output false.

4) Logical XOR(^): Logical XOR operator is used to evaluate the results of two or more expressions at same time.It results in true if one expression is  true then it outputs true. On the other hand, if both the expressions are true and both are false it will return false value as result.
Example:      (3<7) ^  (3!=7),here the first expression checks if 3 is less than 7 , and second will check if 3 is not equal to 7. According to simple maths rule, we know both the expressions are true , So it will result in false. If both are false only then it will output false a result.

5) Conditional AND(&&): Conditional AND operator is used to evaluate the results of two or more expressions at same time.The result of Conditional AND will be true if and only if both the conditions are true else it will return false. It means if exp1 and exp2 are true the output will be true, else if an of the expression is false the value will be false.
Example:      (3>7) &&  (3!=7),here the first expression checks if 3 is greater than 7 , and second will check if 3 is not equal to 7. According to simple maths rule, we know first expression is false and second is true , but it will return  false as it requires both the expressions to be true.

6) Conditional OR (||): Conditional OR operator is used to evaluate the results of two or more expressions at same time.The result of Conditional OR will be false if and only if both the conditions are false else it will return true. It means if exp1 and exp2 are false the output will be false, else if any of the expression is true the value will be true.
Example:(3>7) ||  (3!=7),here the first expression checks if 3 is greater than 7 , and second will check if 3 is not equal to 7.The first expression is false , but the second expression is true so the output will also be true as in Conditional OR , to return false both the expression must be false.


Logical Operators  Output

The figure above will help you to easily understand the output of the operators


Don't get confuse between Conditional AND,OR and Logical AND ,OR. They results the same but conditional operators are preferred by programmers lets see why?

Conditional AND(&&),OR(||) vs Logical AND(&),OR(|)

Both the operators give the same results but the difference lies in their way or working.
  • The Conditional AND (&&) , in the case of false result, checks the left conditional of logical expression only if the left condition is false it will not check the right condition it will just print false as output while on the other hand , the logical AND operator evaluate both conditions , compare their results then print false. This is why  the Conditional AND is preferred as it does not waste time to evaluate both conditions to output false.
  • The Conditional OR (||) , in the case of true result does not waste time in evaluating both conditions and comparing their result to give true as it has rule if the any expression is true it will return true. It only check left condition of expressions and if output is true it prints true while the Logical OR checks both conditions, compare their result then output true. 

Logical Operators Example:

 
Lets see an example of logical operators,

write code as shown below:

Logical Operators example in Java


Output: See the output in Console.

Logical Operators example in Java

Experiment: Alter the > greater than operator to other relational operators as(==,!=,<,<=,>= ) and see the output.

Practice is the key so keep practicing. Happy Coding!

Comments