/** Demo loops Lecture of 07 October 2003 
    We used the Debugger feature of DrJava to step
    through execution of several loops.*/
public class Loops {
    
    /** print out the squares of integers in range 5..8 */
    public static void print8() {
        System.out.println(5*5);
        System.out.println(6*6);
        System.out.println(7*7);
        System.out.println(8*8);
    }
    
    /** print out the squares of integers in range 5..8 */
    public static void print8a() {
        int k= 5;
        while (k != 9) {
            System.out.println(k*k);
            k= k+1;
        }
    }
    
    /** print the squares of integers in range 5..n */
    public static void print8a(int n) {
        int k= 5;
        while (k != n+1) {
            System.out.println(k*k);
            k= k+1;
        }
    }
    
    
    /** = the sum of the values in the range 0..n
          Precondition: n >= 0 */
    public static int sum(int n) {
        int total= 0;
        int k= 0;
        // invariant: total = the sum of values in 0..k-1
        while (k != n+1) {
            total= total + k;
            k= k + 1;
        }// total = sum of o..k-1
        
        return total;
    }
    // = sum of 0..n. Precondition n >= 0
    public static int sumOf(int n){
        int k= 0;
        int x= 0;
        // { invariant: x = sum of 0..k }
        // inv: x =
        while ( k != n ) {   
            k= k+1;   
            x= x + k;
        } // x = sum of 0..n
        
        return x;
    }
    
    
}