DeMorgan’s Laws¶

DeMorgan’s laws were developed by Augustus De Morgan in the 1800s. They show how to handle the negation of a complex conditional.

• not (a and b) is the same as (not a) or (not b)
• not (a or b) is the same as (not a) and (not b)

Notice that an and is changed to an or by negation and an or to an and.

Java uses ! to indicate negation. Applying DeMorgan’s Laws to !(x < 3 && y > 2) means that this complex conditional will be true when (x >= 3 || y <= 2) and !(x < 3 || y > 2) is the same as (x >= 3 && y <= 2).

5-4-1: What is printed when the following code executes and x equals 4 and y equals 3?

if (!(x < 3 || y > 2)) System.out.println("first case");
else System.out.println("second case");


5-4-2: What is printed when the following code executes and x equals 4 and y equals 3?

if (!(x < 3 && y > 2)) System.out.println("first case");
else System.out.println("second case");