// examples of using conditionals in Java public class Condition { public static void main(String[] args) { /* 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(); /* boolean operators */ int p = 6; int q = 1; // setting boolean variables to boolean expressions 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(); /* switch */ int val = 4; // only works on int, char switch (val) { case 1: case 2: case 3: // do if 1, 2, 3 System.out.println("one, two, or three"); case 4: case 5: // do if 1, 2, 3, 4, 5 System.out.println("five or less"); break; // no more cases included case 7: // only for case 7 System.out.println("lucky seven"); break; default: // all other cases System.out.println("some number"); } System.out.println(); String productCode = ""; // String, int, char double total = 0; switch (productCode) { case "DLXTALL": System.out.print("Deluxe "); total += 1.5; // continue to next case case "TALL": System.out.println("Tall"); total += 6; break; // no more cases case "SPMIN": System.out.println("Super "); total += .8; // continue to next case case "MINI": System.out.println(" Mini"); total += 4; break; // no more cases default: System.out.println("Other Product "); total += 5; break; // decorative } /* Conditional operator */ int x = 15; // determine result // iffy use of conditional op to do main calculation int result = (x > 10) ? 20 : 0; System.out.println(result); System.out.println(); // equivalent if statement // easier to read, easier to add code to if (x > 10) { // can't add code like this with the operator //System.out.println("It was big"); result = 20; } else { result = 0; } // add bonus if appropriate // better use of conditional op // to handle minor part of more complex code boolean bonus = false; result = 54 * x * x + 13 * x - 123 + (bonus ? 100 : 0); System.out.println(result); System.out.println(); // probably better way to do it: int bonusval = 100; // can set bonusval to whatever is appropriate result = 54 * x * x + 13 * x - 123 + bonusval; // format correctly for x singular or plural // smart use of conditional op // to handle unimportant detail // not easy reading System.out.println("There " + ((x == 1) ? "is " : "are ") + x + " light" + ((x == 1) ? "" : "s")); } }