public class Demo {
    
    // 2..3  is the range consisting of 2,3
    // 2..2  is the range consisting of 2
    // 2..1  is the range consisting of no integers
    
    /* Pattern to process a range of integers a..b:
     for (int i= a; i <= b; i= i+1) {
     Process integer i
     }*/
    
    /** = sum of squares in range 2..n */
    public static int squares(int n) {
        int x= 0;
        for (int i= 2; i <= n; i= i+1) {
            // Process i (add i*i to x)
            x= x + i*i;
        }
        
        return x;
    }
    
    /** Print squares of 3..10 */
    public static void printSquares() {
        for (int i= 3; i <= 10; i= i+1) {
            // Process i
            System.out.println(i*i);
        }
    }
    
    /* Pattern to process a range of integers c..d-1:
     for (int i= c; i != d; i= i+1) {
     Process integer i
     }
     
     
     /** Print the indices of all 'e's in s 
     Indices of s are 0, 1, ..., s.length()-1*/
    public static void printe(String s) {
        for (int i= 0; i != s.length(); i= i+1) {
            //Process i
            // if s[i] is 'e', then print i
            if (s.charAt(i) == 'e') {
                System.out.println(i);
            }
        }
        
    }
    
    /** = index of first 'e' in s (s.length() if none) */
    public static int firstE(String s) {
        int i;
        for (i= 0; i != s.length(); i= i+1) {
            //Process i
            // if s[i] is 'e', then return i
            if (s.charAt(i) == 'e') {
                return i;   
            }
            
        }
        return i;
    }
    
    
    /** = the sum of 1/1 + 1/2 + 1/3 + ... + 1/n */
    public static double sum1(int n) {
        double v= 0;
        // v is the sum of values so far
        for (int i= 1; i <= n; i= i+1) {
            // Process i
            v=  v + 1.0/i;
        }
        
        return v;
    }
    
    /** = the sum of 1/1 - 1/2 + 1/3 - 1/4 + ... + 1/n */
    public static double sum2(int n) {
        double v= 0; 
        int sign= 1; // if i is odd, sign is 1; otherwise, -1
        // v is the sum of values so far
        // v is the sum of values so far
        for (int i= 1; i <= n; i= i+1) {
            // Process i
            v= v + sign*1.0/i;
            sign= - sign;
        }
        
        return v;
    }
    
    /** = the sum of 1/1 - 1/2 + 1/3 - 1/4 + ... + 1/n */
    public static double sum3(int n) {
        double v= 0;
        for (int i= 1; i <= n; i= i+1) {
            // Process i
            if (i%2 == 0)
                v= v - 1.0/i;
            else v= v + 1.0/i;
        }
        return v;
    }
    
    
}