In the previous section, we have learned what are Relational operators. Relational operators are often used with Logical Operators to construct more complex decision making expressions. In this unit, let's use them in combination with logical operators.
Let's start learning Logical Operators.
The Logical operators are used to perform logical operations on boolean values. These will also produce a true or false result. In Java, Logical Operators work in the same way they do in Python, with only a difference in syntax.
Logical Operator |
Syntax in Java |
Syntax in Python |
AND |
&& |
and |
OR |
| | |
or |
NOT |
! |
not |
Logical AND is a binary operator (an operator that operates on two operands).
condition1 && condition2 |
It gives true, only if both the conditions are true else, it gives false
condition1 |
condition2 |
condition1 && condition2 |
true |
true |
true |
false |
false |
false |
false |
true |
false |
true |
false |
false |
If the first condition is false, the second condition will not be evaluated and the expression will return false
Example 1:
class Main { public static void main(String[] args) { System.out.println((2 < 3) && (1 < 2)); } } |
true
Step by Step Explanation
(2 < 3) && (1 < 2) true && (1 < 2) true && true true |
Example 2:
class Main {public static void main(String[] args) { System.out.println((10 < 5) && (6 < 9)); } } |
false
Step by Step Explanation
(10 < 5) && (6 < 9) false && (6 < 9) false |
As the first condition is false, the second condition is not checked.
Logical OR is also a binary operator. It gives true if any one of the conditions is true else, it gives false
condition1 || condition2 |
condition1 |
condition2 |
condition1 || condition2 |
false |
false |
false |
false |
true |
true |
true |
false |
true |
true |
true |
true |
If the first condition is true, the second condition will not be evaluated and the expression will return true
Example 1:
class Main { public static void main(String[] args) { System.out.println((12 > 13) || (15 < 12)); } } |
false
Step by Step Explanation
(12 > 13) || (15 < 12) false || (15 < 12) false || false false |
Example 2:
class Main { public static void main(String[] args) { System.out.println((2 < 3) || (2 < 1)); } } |
true
Step by Step Explanation
(2 < 3) || (2 < 1) true || (2 < 1) true |
As the first condition is true, the second condition is not checked.
Logical NOT is a Unary operator (an operator that operates only on one operand). It gives the opposite value of the given boolean.
!(condition) |
condition |
!(condition) |
false |
true |
true |
false |
Example 1:
class Main { public static void main(String[] args) { System.out.println(!(5 > 7)); } } |
true
Step by Step Explanation
!(5 > 7) !(false) true |
Example 2:
class Main { public static void main(String[] args) { System.out.println(!(2 < 3)); } } |
false
Step by Step Explanation
!(2 < 3) !(true) false |
Logic expressions are very helpful when writing conditionals for if...else, while and do-while statements, which we'll look at in the upcoming units.
Conclusion:
Logical AND Operator gives true if both the conditions are true.
Logical OR Operator gives true if any of the conditions are true.
Logical NOT Operator gives the opposite of the boolean value