Computer Science 15-100, Fall 2008
Class Notes: Conditionals
Conditionals
class MyCode { public static void main(String[] args) { if (2 < 1) { if (3 == 3) System.out.println("a"); else System.out.println("b"); } else { if (4 == 4) System.out.println("c"); else System.out.println("d"); } } }
class MyCode {
public static void main(String[] args) {
if (2 < 1)
if (3 == 3)
System.out.println("a");
else // dangling else!
System.out.println("b");
}
}
Remedy:
class MyCode {
public static void main(String[] args) {
if (2 < 1) {
if (3 == 3)
System.out.println("a");
}
else
System.out.println("b");
}
}
class MyCode { public static void main(String[] args) { System.out.println((1 < 2) ? "yes" : "no"); int x = ((3 > 4) ? 5 : 6); System.out.println(x); } }
class MyCode {
public static void main(String[] args) {
int p = 5;
System.out.println("I saw " + p + " " + ((p == 1) ? "person" : "people"));
}
}
Equivalent to:
class MyCode { public static void main(String[] args) { int p = 5; System.out.print("I saw " + p + " "); if (p == 1) System.out.println("person"); else System.out.println("people"); } }
Wrong | Right |
class MyCode { public static void main(String[] args) { boolean b = true; if (!b) System.out.println("no"); else System.out.println("yes"); } } |
class MyCode { public static void main(String[] args) { boolean b = true; if (b) System.out.println("yes"); else System.out.println("no"); } } |
Wrong | Right |
class MyCode { public static void main(String[] args) { boolean b = false; if (b) { // do nothing } else System.out.println("no"); } } |
class MyCode { public static void main(String[] args) { boolean b = false; if (!b) System.out.println("no"); } } |
Wrong | Right |
class MyCode { public static void main(String[] args) { boolean b1 = true; boolean b2 = true; if (b1) if (b2) System.out.println("Both!"); } } |
class MyCode { public static void main(String[] args) { boolean b1 = true; boolean b2 = true; if (b1 && b2) System.out.println("Both!"); } } |
Wrong | Right |
class MyCode { public static void main(String[] args) { boolean b = true; if (b) System.out.println("yes"); if (!b) System.out.println("no"); } } |
class MyCode { public static void main(String[] args) { boolean b = true; if (b) System.out.println("yes"); else System.out.println("no"); } } |
Another Example:
Wrong | Right |
class MyCode { public static void main(String[] args) { int x = 10; if (x < 5) System.out.println("small"); if ((x >= 5) && (x < 10)) System.out.println("medium"); if ((x >= 10) && (x < 15)) System.out.println("large"); if (x >= 15) System.out.println("extra large"); } } |
class MyCode { public static void main(String[] args) { int x = 10; if (x < 5) System.out.println("small"); else if (x < 10) System.out.println("medium"); else if (x < 15) System.out.println("large"); else System.out.println("extra large"); } } |
Yet Another Example:
Wrong | Right |
class MyCode { public static void main(String[] args) { char c = 'a'; if ((c >= 'A') && (c <= 'Z')) System.out.println("Uppercase!"); if ((c >= 'a') && (c <= 'z')) System.out.println("lowercase!"); if ((c < 'A') || ((c > 'Z') && (c < 'a')) || (c > 'z')) System.out.println("not a letter!"); } } |
class MyCode { public static void main(String[] args) { char c = 'a'; if ((c >= 'A') && (c <= 'Z')) System.out.println("Uppercase!"); else if ((c >= 'a') && (c <= 'z')) System.out.println("lowercase!"); else System.out.println("not a letter!"); } } |
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem