Computer Science 15-110, Spring 2010
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!"); } } |
import java.util.*; class MyCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your choice (0-9): "); int choice = scanner.nextInt(); switch (choice) { case 0: System.out.println("Case for choice 0."); break; case 1: System.out.println("Case for choice 1."); break; // multiple cases: case 5: case 8: System.out.println("Case for choice 5 or 8."); // no break, fall-through case 9: System.out.println("Case for choice 9."); break; default: System.out.println("Case for default choice."); } } }
class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = " + numDays);
}
}
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem