public class ForDemo {
  
  /**
   * initialization;
   * while(condition) {
   *   body
   *   updatestatement
   * }
   * 
   * for(initialization; condition; update statement) {
   *   body
   * }
   * 
   * For loops are good to use when you know the range
   * and bad otherwise.
   */ 
  
  /** 
   * Return the string s with the characters reversed.
   */ 
  public static String reverse(String s) { 
    String rev = "";
    
    for (int i = 0; i < s.length(); i++) {
      rev = s.charAt(i) + rev;
    }
    
    return rev;
  }
  
  /**
   * Print the squares of integers in the range
   * 1 to n.
   */
  public static void printSquares(int n) {
    for (int i = 1; i <= n; i++) {
      System.out.println(i * i);
    }
  }
  
  /**
   * Return  true if the string is a palindrome (i.e.
   * its reverse is the same as itself. Otherwise, return false. 
   */
  public static boolean isPalindrome(String s) {
    for (int i = 0; i < s.length() / 2; i++) {
      if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
        return false;
      }
    }
    return true;
  }
  
  /**
   * What is the displayed by this method?
   */
  public static void example1() {
    int i=0, j=0;
    for (i = 0; i < 4; i++) {
      for (j = 0; j < 2; j++) {
        System.out.print("i is:" + i + " j is:" + j + ". ");
      }
      System.out.println();
    }
    System.out.println(i + "," + j);
  }

  /**
   * How many times does the outer loop execute?
   * 
   * What is displayed by this method?
   */
  public static void example2() {
    int i = 0;
    while (i <= 3) {
      for (int j = 1; j < 4; j++) { 
        if (i == j) {
          i++;
        }
      }      
      System.out.println("Inside: " + i);
      i++;
    }
    System.out.println("Outside: " + i);
  }
  
  // exercise: change the j < 4 in for loop to j < 2, what happens?

}