Lecture 8, CS 302-6, September 21

 

  1. Review
    1. Strings
    2. Decisions

                                                               i.      Tree structure

    1. Comparisons
    2. From end of last class:

                                                               i.      Substring of end, beg index, eg substring(3,1)

                                                             ii.      Ifs without elses are ok

1.      We’ll show an example of this eventually

  1. Decisions
    1. If/Else
    2. Else If
    3. Nested branches

                                                               i.      Example – Birthday.java

  1. Switch statement
    1. Another way for writing long chains of if elses
    2. Only ints/chars allowed as input to switch()

                                                               i.      (actually, in Java 7, String is allowed as well)

    1. Only = = operador is used by case.  >, !=, etc not supported
    2. Structure:

switch (digit)

{

      //Execute case 1 if digit==1

      case 1:  do something; break;

      //do case 2 if digit==2

case 2 : do something else; break;

//If digit !=1 and digit !=2 do this

default: do the default behavior; break;

}

  1. Booleans
    1. For storing whether a logical condition is true or false.
    2. 1==1?  True!  1+1==1?  False!
    3. Possible values: true, false

boolean bool=true;

boolean bool2=(1==1); //true

boolean bool3=false;

boolean bool4=(1+1==1); //false

    1. Use in conditions:

                                                               i.      if(bool)…

  1. Boolean operators
    1. Boolean operators allow us to program more logic into our conditions

                                                               i.      && -> And

                                                             ii.      || -> Or

                                                            iii.      ! -> Not

    1. Boolean truth tables

                                                               i.      A&&B, A ||B, !A

    1. Order: !, then &&, then ||

                                                               i.      A || B && !C means A || (B && (!C))

                                                             ii.      Rule of thumb: When in doubt, use ()

    1. Note – exams will have precedence table
    2. Implicit Booleans: if(1==2 || 3>2) etc

                                                               i.      1==2 and 3>2 can both be interpreted as booleans

  1. HW
    1. R3.7, R3.8, R3.19
    2. P3.9, P3.24