Lecture 11, CS 302-6, September 28

 

  1. Review
    1. Loops

                                                               i.      While

                                                             ii.      For

                                                            iii.      Do…while

  1. Do Loop
    1. Almost identical to a While loop.

                                                               i.      Doesn’t check condition before first iteration

                                                             ii.      Post-test instead of pre-test

    1. Syntax:

do

{

}

while(condition);

    1. One use – input validation.

                                                               i.      You know you need to get an input at least once

                                                             ii.      You want to keep going while you haven’t met the necessary condition

                                                            iii.      So, you want to get the input within the loop – but you don’t know beforehand what the value will be

    1. Example – InputValidation.java
  1. Comparing different types of loops
    1. While

                                                               i.       Indefinite

                                                             ii.      Aren’t required to do it

                                                            iii.      For generic use

1.      One example: bank account.  Keep going until it’s empty, but you don’t know when that will happen

    1. Do

                                                               i.      Indefinite

                                                             ii.      Required to do it at least once

                                                            iii.      Examples – input validation

    1. For

                                                               i.      Definite

                                                             ii.      Aren’t required to do it

                                                            iii.      Examples – counting, iterating

  1. Sentinel values
    1. What is a sentinel value?

                                                               i.      A value for input that indicates that we are done accepting values.  The border between our data set and everything else.

                                                             ii.      Real life example – period at the end of a sentence?

                                                            iii.      Computer example – in a menu, quit

    1. Different ways to do this:

                                                               i.      while(value!=some number)

                                                             ii.      while(!done)

                                                            iii.      while(in.hasNextDouble()), while(in.hasNextInt()), etc.

    1. Example – Sentinels.java
    2. WHAT NOT TO DO – Break statements
  1. Things you can do with loops
    1. Sums, averages

                                                               i.      Standard deviation, variance, etc

    1. Counting matches

                                                               i.      For each character in a string if it is the character we are looking for, add one to a counter

    1. Finding the first match

                                                               i.      Determining if a word contains a letter, substring, capital, etc

    1. Max and min

                                                               i.      Keep track of largest, smallest as you go

    1. Comparing adjacent values

                                                               i.      One pointer to previous, one to current

    1. Example – Fibonacci.java

                                                               i.      Examples in nature (?): Rabbit reproduction, nautilus shells, sunflower flowers, golden ratio, etc

1.      F0=0,F1=1, fn=fn-1+fn-2 for n>1

    1. Example – Factor.java
  1. HW
    1. Finish reading ch. 4