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); // trueThe 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); // trueChallenge
BeginnerYou're helping a pet shop create a system to determine if they can sell a pet to a customer.
Initialize the following variables:
hasLicensewith the valuetruehasSpacewith the valuefalsehasExperiencewith the valuetrue
Write the following logical expressions to determine if:
canSellRegularPet: Customer needs EITHER a license OR experience, AND must have spacecanSellExoticPet: Customer needs BOTH a license AND experience, AND must have spacecannotSellAnyPet: Customer has NO license AND NO experience, OR has NO spaceresult:canSellRegularPetORcanSellExoticPetORcannotSellAnyPet
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);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else