Mark Danishevsky

Tutorials: Boolean Expressions

Back to tutorials home

Boolean Expressions Navigation

if, else if, and else

Consider the following code segment:

In the code segment:

Logical Operators

Learn about combining conditions using logical operators like &&, ||, and !.

Logical Operators
Java Name Logic Table
&& Logical AND
Inputs true false
true true false
false false false

|| Logical OR
Inputs true false
true true true
false true false

! Logical NOT
Input= true false
false true

De Morgan's Laws

De Morgan's Laws
Rule English Symbolic Form
Law 1 “Not (A and B) is the same as (Not A or Not B)” !(A && B) = !A || !B
Law 2 “Not (A or B) is the same as (Not A and Not B)” !(A || B) = !A && !B

(De Morgan's Law - Theorem, Proofs, Formula & Examples | GeeksforGeeks)

We can use De Morgan's laws to simplify complex logical operations.

For example, let's say we have the following logical operation:
!(sunny || windy)

We see that it is very hard to understand what the above logical expression really means, however, with De Morgan's laws we can easily find out what the expression simplifies to. We first apply the 2nd Law (!(A || B) ≡ !A && !B):

!(sunny || windy) → !sunny && !windy