package cis119_07conditionals; public class BooleanExamples { public static void main(String[] args) { /* boolean operators */ System.out.println("!true = " + !true); // false System.out.println("!false = " + !false); // true System.out.println("false && false = " + (false && false)); //false System.out.println("false && true = " + (false && true)); //false System.out.println("true && false = " + (true && false)); //false System.out.println("true && true = " + (true && true)); //false System.out.println("false || false = " + (false || false)); //false System.out.println("false || true = " + (false || true)); //true System.out.println("true || false = " + (true || false)); //true System.out.println("true || true = " + (true || true)); //true /* nested if-else */ boolean condition1 = true; boolean condition2 = false; if (condition1) { // nesting lets us add code appropriate for condition1 // whichever way condition2 comes out. System.out.println("condition 1 is true"); // then check condition2 if (condition2) { System.out.println("both are true"); } else { System.out.println("1 but not 2"); } } else { System.out.println("condition 1 is false"); if (condition2) { System.out.println("2 but not 1"); } else { System.out.println("both false"); } } System.out.println(); /* conditionals with booleans */ // does the same thing as the previous // but no nesting for condition1 independent of condition2 if (condition1 && condition2) { System.out.println("both are true"); } else if (condition1) { System.out.println("1 but not 2"); } else if (condition2) { System.out.println("2 but not 1"); } else { System.out.println("both false"); } System.out.println(); // setting boolean variables to boolean expressions int p = 6; int q = 1; boolean b = p == 1; boolean d = q < 5 || b; if (b && d) { System.out.println(" both true"); } // demorgan's law if (!(!b || !d)) { System.out.println(" same thing"); } if (b || d) { System.out.println(" at least one true"); } // demorgan's law if (!(!b && !d)) { System.out.println(" same thing"); } System.out.println(); } }