Lecture 9, CS 302-7 and 8, February 10

 

  1. Reminders
    1. Program 1

                                                               i.      Due Feb 24

                                                             ii.      Partners Feb 17

                                                            iii.      Piazza

  1. Review
    1. Decisions

                                                               i.      Multiple alternatives and nested conditions

    1. Switch statements
    2. Booleans

                                                               i.      boolean <name> = <true or false>;

  1. Boolean operator
    1. Allow us to program more logic into our conditions
    2. &&,||,!

                                                               i.      And, Or, Not

    1. Boolean truth tables

                                                               i.      A and B are conditions

1.      eg x>5, y==4

                                                             ii.      A&&B, A ||B, !A

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

                                                               i.      A || B && !C -> 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
    3. Side notes:

                                                               i.      Simplifications

1.      !(x=y) -> x!=y

2.      ! (x>y) -> x<=y

                                                             ii.      DeMorgan’s law

1.      !(A && B)=>!A || !B

2.      !(A || B) =>!A && !B

                                                            iii.      Lazy evaluation

1.      example - if(quantity>0 && price/quantity)

2.      Once first half is false, does not need to compute second half

3.      Once common use is to avoid /0 errors, like in above

  1. Input validation
    1. scanner:

                                                               i.      hasNextInt

                                                             ii.      hasNextDouble

                                                            iii.      hasNext

                                                           iv.      hasNextLine

  1. While loops
    1. Use for doing things multiple times
    2. Event controlled – keep going until some event occurs
    3. Condition-based

                                                               i.      Condition can be either explicit or implicit boolean

    1. Indefinite – we don’t always know ahead of time when exactly they will end
    2. Pretty much the same syntax as plain if statements, except replace if with while
    3. IF LOOPS DON’T EXIST
    4. Watch out for infinite loops
  1. HW
    1. Read and understand 4.1-4.3