This book is now obsolete Please use CSAwesome instead.

5.5. 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, which is a conditional statement with more than one condition joined by an and (&&) or or (||), such as (x < 3) && (y > 2).

  • not (a and b) is the same as (not a) or (not b). In Java this is written as !(a && b) == !a || !b

  • not (a or b) is the same as (not a) and (not b). In Java this is written as !(a || b) == !a && !b

Applying DeMorgan’s Laws to !(x < 3 && y > 2) yields !(x < 3) || !(y > 2) which means that this complex conditional will be true when (x >= 3 || y <= 2).

Note

Notice that the negation is distributed to all parts of a complex conditional. It negates each part of the conditional and changes and (&&) to or (||) and or (||) to and (&&).

The negation modifies each conditional as shown below.

  • < becomes >=

  • > becomes <=

  • == becomes !=

  • <= becomes >

  • >= becomes <

  • != becomes ==

See the example below. For what values of x and y will it print true? Try out different values of x and y to check your answer.

For more information about DeMorgan’s laws see http://en.wikipedia.org/wiki/De_Morgan’s_laws.

You have attempted of activities on this page