Computer Science 15-100, Fall 2008
Class Notes:  Conditionals


  1. The "if" statement
    1. "if'
    2. Blocks
      1. A block is a statement
      2. Two ways to indent a block
      3. Indenting alone does not form blocks
    3. The empty statement
      1. A semicolon can be an empty statement (bad idea)
      2. A block can be an empty statement
    4. "if-else"
    5. "if-else-if-...-else"
    6. Nested "if" statements
    7. The "dangling-else" problem
  2. The conditional (ternary) operator (?:)
    1. An Example
    2. A Better Example
  3. Incorrect usage
    1. Negated Condition (with "else" clause)
    2. Empty "if" Clause
    3. Using "if" instead of &&
    4. Avoiding "else"
  4. Practice problems

Conditionals

  1. The "if" statement
     
    1. "if'

      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

        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

        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 ...

        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

        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)

        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

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

         
    4. "if-else"

      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"

      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:

      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
      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
      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");
        }
      }
  2. The conditional (ternary) operator (?:)
     
    1. An Example
      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
      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");
        }
      }
  3. Incorrect usage
     
    1. Negated Condition (with "else" clause)
       
      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");
        }
      }

       

    2. Empty "if" Clause
       
      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");
        }
      }

       

    3. Using "if" instead of &&
       
      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!");
        }
      }

       

    4. Avoiding "else"
       
      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!");
        }
      }

       

  4. Practice problems

    Write the following methods:
    1. isEven, isOdd, isPositive, ...
    2. signum (replaces Math.signum)
    3. etc...

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem