Menu
Coddy logo textTech

Logical Operators Part 4

Part of the Fundamentals section of Coddy's Java journey — lesson 26 of 73.

De Morgan's Law is a rule that helps us simplify expressions with NOT in them. When working with logical expressions, sometimes we need to simplify or rearrange them.

When you have not in front of two conditions joined by and, you can split it into two separate parts. The and becomes or, and each part gets its own not:

!(A && B) is the same as (!A) || (!B)

For example:

// Let's check if a number is NOT (between 1 and 10)
int number = 15;

// These two expressions are equivalent:
boolean result1 = !(number >= 1 && number <= 10);
boolean result2 = !(number >= 1) || !(number <= 10);

System.out.println(result1);  // true
System.out.println(result2);  // true

The opposite is also correct: !(A || B) is the same as (!A) && (!B)

For example:

// Checking if a person is NOT (a student or employed)
boolean isStudent = false;
boolean isEmployed = false;

// These two expressions are equivalent:
boolean result1 = !(isStudent || isEmployed);
boolean result2 = !(isStudent) && !(isEmployed);

System.out.println(result1);  // true
System.out.println(result2);  // true
challenge icon

Challenge

Beginner

You're helping a pet shop create a system to determine if they can sell a pet to a customer.

Initialize the following variables:

  • hasLicense with the value true
  • hasSpace with the value false
  • hasExperience with the value true

Write the following logical expressions to determine if:

  • canSellRegularPet: Customer needs EITHER a license OR experience, AND must have space
  • canSellExoticPet: Customer needs BOTH a license AND experience, AND must have space
  • cannotSellAnyPet: Customer has NO license AND NO experience, OR has NO space
  • result: canSellRegularPet OR canSellExoticPet OR cannotSellAnyPet

Tip: Write the condition that the requirement describes, not the value the variable happens to hold. "Must have space" → hasSpace; "has NO space" → !hasSpace. Use ! only when the requirement is the opposite of what the variable name states, such as "has NO license" → !hasLicense.

Try it yourself

public class Main {
    public static void main(String[] args) {
        // Initialize variables
        

        // Calculate conditions
        boolean canSellRegularPet = 
        boolean canSellExoticPet = 
        boolean cannotSellAnyPet =
        boolean result =

        // Don't delete the lines below
        System.out.println("Can sell regular pet: " + canSellRegularPet);
        System.out.println("Can sell exotic pet: " + canSellExoticPet);
        System.out.println("Cannot sell any pet: " + cannotSellAnyPet);
        System.out.println("Result: " + result);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals