1. The "if" statement
    1. "if'
      public class MyCode { public static void main(String[] args) { if (1 < 2) System.out.println("yes"); if (5 < 4) System.out.println("really?"); } }
    2. Blocks
      1. A block is a statement

        public class MyCode { public static void main(String[] args) { if (1 < 2) { System.out.println("yes"); System.out.println("yep"); System.out.println("sure!"); } if (5 < 4) { System.out.println("really?"); System.out.println("hardly!"); } } }
      2. Two ways to indent a block

        public class MyCode { public static void main(String[] args) { if (1 < 2) { // open brace on same line as test System.out.println("yes"); System.out.println("yep"); } } }

        ... or ...

        public class MyCode { public static void main(String[] args) { if (1 < 2) { // open brace on new line after test System.out.println("yes"); System.out.println("yep"); } } }
      3. Indenting alone does not form blocks
        public class MyCode { public static void main(String[] args) { if (5 < 4) System.out.println("really?"); System.out.println("hardly!"); System.out.println("surprising!"); } }
    3. The empty statement
       
      1. A semicolon can be an empty statement (bad idea)

        public class MyCode { public static void main(String[] args) { if (5 < 4); // ERROR: Extra semicolon! System.out.println("really?"); } }
      2. A block can be an empty statement

        public class MyCode { public static void main(String[] args) { if (5 < 4) { // empty block (do nothing) } System.out.println("really?"); } }
    4. "if-else"

      public class MyCode { public static void main(String[] args) { if (5 < 4) System.out.println("really?"); else System.out.println("not really!"); } }
    5. "if-else-if-...-else"

      public class MyCode { public static void main(String[] args) { if (5 < 4) System.out.println("really?"); else if (4 < 3) System.out.println("curious!"); else if (3 > 2) System.out.println("you bet!"); else System.out.println("why not?"); } }


      A Better Example:

      public class MyCode { public static void main(String[] args) { int n = -3; if (n > 0) System.out.println("positive"); else if (n == 0) System.out.println("zero"); else System.out.println("negative"); } }
    6. Nested "if" statements
      public 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"); } } }
    7. The "dangling-else" problem
      public 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:

      public class MyCode { public static void main(String[] args) { if (2 < 1) { if (3 == 3) System.out.println("a"); } else System.out.println("b"); } }
  2. The conditional (ternary) operator (?:)
     
    1. An Example
      public 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); } }
    2. A Better Example
      public class MyCode { public static void main(String[] args) { int p = 5; System.out.println("I saw " + p + " " + ((p == 1) ? "person" : "people")); } }

      Equivalent to:

      public 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"); } }
  3. Incorrect usage
     
    1. Negated Condition (with "else" clause)
       
      Wrong Right
      public class MyCode { public static void main(String[] args) { boolean b = true; if (!b) System.out.println("no"); else System.out.println("yes"); } }
      public class MyCode { public static void main(String[] args) { boolean b = true; if (b) System.out.println("yes"); else System.out.println("no"); } }

       

    2. Empty "if" Clause
       
      Wrong Right
      public class MyCode { public static void main(String[] args) { boolean b = false; if (b) { // do nothing } else System.out.println("no"); } }
      public class MyCode { public static void main(String[] args) { boolean b = false; if (!b) System.out.println("no"); } }

       

    3. Using "if" instead of &&
       
      Wrong Right
      public class MyCode { public static void main(String[] args) { boolean b1 = true; boolean b2 = true; if (b1) if (b2) System.out.println("Both!"); } }
      public class MyCode { public static void main(String[] args) { boolean b1 = true; boolean b2 = true; if (b1 && b2) System.out.println("Both!"); } }

       

    4. Avoiding "else"
       
      Wrong Right
      public class MyCode { public static void main(String[] args) { boolean b = true; if (b) System.out.println("yes"); if (!b) System.out.println("no"); } }
      public 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
      public 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"); } }
      public 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
      public 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!"); } }
      public 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!"); } }

       

  4. The switch Statement
    • Example:
      import java.util.*; public 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."); } } }
    • Cases must be compile-time constants

      wrong:
      char c = s.charAt(0); char d = 'X'; switch (c) { case d : System.out.println("X marks the spot!"); break; } right: char c = s.charAt(0); switch (c) { case 'X': System.out.println("X marks the spot!"); break; }
    • A mildly compelling example from Sun:
      See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html:
      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); } }