CS51 - Fall 2009 - Lecture 23

  • while loops
       - while(<condition>){
          // do the following
       }
       - as long as <condition> is true, continue in the loop

  • Take a look at our FallingLeaves demo
       - what pattern did we use to generate 10 leaves in Tree.java?
       - let's look at the construction of the while loop in Tree.java
          - What role does the treeCount variable play?
          - initialize a variable for counting
          - while loop
             - conditioned on the counter
             - inside the while loop we increment the count

          <initial_statement>
          while( <condition> ){
             // do the following
             <update_statement>
          }

  • For loops
       - don't add any functionality to the language
       - makes our life easier
       - makes code easier to understand for certain types of loops
       - for( <initial_statement>; <condition>; <update_statement>){
          // do the following
       }
       - When we first execute the for loop, execute <initial_statement>
          - often used to initialize a counter variable
          - only happens once
       - As long as <condition> is true, continue in the loop
       - At the end of each iteration in the loop, execute <update_statement>
          - often used to increment a counter variable
          - happens EVERY iteration

  • How can we change our Tree class to use a for loop?

  • Show Knitting demo again

  • Show Knitting code
       - class: change this code to use for loops

  • Look at Recursive Fibonacci again
       private int Fib(int n){
          if( n <= 2 ){
             return 1;
          }else{
             return Fib(n-1) + Fib(n-2);
          }
       }
       - How efficient is Fibonacci?

  • Look at Fibonacci without recursion
       - Do Fib(10) on paper...
       - How did you do it?
       - Started at 1, 1, then wrote the numbers out
       - We'd like to write a function that does it this way
       - What do we need?
          - We need some way of keeping track of the intermediary numbers
       - How many numbers do we have?
          - n, if we include the final number
       - Arrays allow us to keep track of a sequence of things, using just one variable
       - Arrays are just like other variables:
          - They have a declaration
             - <type_of_thing>[] <variable_name>;
             - int[] a;
             - double[] b;
             - FramedRect[] c;
          - We can create new arrays - we must specify the length!
             - a = new int[10];
             - b = new FramedRect[1000];
          - Array variables are like any other variables, b's type is "FramedRect[]" for example
          - An array, contains a set of index variables
             - a[index] is just another variable!
             - They are indexed starting at 0, so 0 is the first element, 1 the second, etc
             - We can assign to individual elements of arrays
                - a[0] = 50;
                - b[30] = new FramedRect(...);
             - We can access elements in an array
                - int val = a[1] + a[2];
                - b[30].move(...);
       - Back to Fibinacci... How do arrays help us?

       private int Fib(int n){
          int[] fib = new int[n];
          
          fib[0] = 1;
          fib[1] = 1;
          
          for( int i = 2; i < n; i++ ){
             fib[i] = fib[i-1] + fib[i-2];
          }
          
          return fib[n-1];
       }

  • Show SlidesF09 demo: how can we do this? Hint: use arrays!

  • Look through SlidesF09 code