1. De Morgan's Laws
De Morgan's Laws
De Morgan's laws allow you to freely convert two types of boolean expressions:
public class MyProgram {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
//These two are the same:
System.out.println( !( a && b ) ); //NOT (a AND b)
System.out.println( !a || !b ); //(NOT a) OR (NOT b)
//These two are the same:
System.out.println( !( a || b ) ); //NOT (a OR b)
System.out.println( !a && !b ); //NOT a AND NOT b
}
}
These rules can also be used to break up longer expressions, such as:
public class MyProgram { public static void main(String[] args) { boolean a = true; boolean b = true; //try setting any one to false boolean c = true; boolean d = true; //If one is false, then the whole expression is true: System.out.println( !( a && b && c && d ) ); System.out.println( !a || !b || !c || !d ); } }