DEV Community

Cover image for Logical Operators
Paul Ngugi
Paul Ngugi

Posted on

Logical Operators

The logical operators !, &&, ||, and ^ can be used to create a compound Boolean expression. Sometimes, whether a statement is executed is determined by a combination of several conditions. You can use logical operators to combine these conditions to form a compound Boolean expression. Logical operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value. Here's a list of the Boolean operators.

Image description

The not (!) operator, which negates true to false and false to true. The and (&&) operator. The and (&&) of two Boolean operands is true if and only if both operands are true. The or (||) operator. The or (||) of two Boolean operands is true if at least one of the operands is true. The exclusive or (^) operator. The exclusive or (^) of two Boolean operands is true if and only if the two operands have different Boolean values.

Image description

Image description

Image description

Image description

The program below checks whether a number is divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both.

Image description

In mathematics, the expression
1 <= numberOfDaysInAMonth <= 31
is correct. However, it is incorrect in Java, because 1 <= numberOfDaysInAMonth is evaluated to a boolean value, which cannot be compared with 31. Here, two operands (a boolean value and a numeric value) are incompatible. The correct expression in Java is

(1 <= numberOfDaysInAMonth) && (numberOfDaysInAMonth <= 31)
Enter fullscreen mode Exit fullscreen mode

De Morgan’s law, named after Indian-born British mathematician and logician Augustus De Morgan (1806–1871), can be used to simplify Boolean expressions. The law states:

!(condition1 && condition2) is the same as
 !condition1 || !condition2
!(condition1 || condition2) is the same as
 !condition1 && !condition2
Enter fullscreen mode Exit fullscreen mode

For example,

! (number % 2 == 0 && number % 3 == 0)
Enter fullscreen mode Exit fullscreen mode

can be simplified using an equivalent expression:

(number % 2 != 0 || number % 3 != 0)
Enter fullscreen mode Exit fullscreen mode

As another example,

!(number == 2 || number == 3)
Enter fullscreen mode Exit fullscreen mode

is better written as

number != 2 && number != 3
Enter fullscreen mode Exit fullscreen mode

If one of the operands of an && operator is false, the expression is false; if one of the operands of an || operator is true, the expression is true. Java uses these properties to improve the performance of these operators. When evaluating p1 && p2, Java first evaluates p1 and then, if p1 is true, evaluates p2; if p1 is false, it does not evaluate p2. When evaluating p1 || p2, Java first evaluates p1 and then, if p1 is false, evaluates p2; if p1 is true, it does not evaluate p2. In programming language terminology, && and || are known as the short-circuit or lazy operators. Java also provides the unconditional AND (&) and OR (|) operators

Top comments (0)