/**
 * Various examples of loops.
 *
 * The follow code snippets are solutions to the following problems:
 *
 * 1) Echo each line the user types until they enter nothing.
 * 2) Print out all the words to the song "99 Bottles of Beer"
 * 3) Print all the powers of 2 that are less than 5000.
 * 4) Print backwards all the positive even integers starting with 100.
 *    (That is: 100, 98, 96, 94.... 2.)
 * 5) Print the numbers 1 through 10 on one line, with a comma after each
 *    except the last one.  So, the list should be look exactly like:
 *    1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
 *
 * C) Hi-Lo number game: The computer picks an int between 1 and 100
 *    and then tells the user whether each guess is too high or low until
 *    the user guesses the number correctly.
 *
 * @author Zach Tomaszewski
 * @version 06 Oct 2009
 */
public class LoopExamples {

  public static void main(String[] args) {

    //creating a Scanner without importing it, for use in some examples
    java.util.Scanner keybd = new java.util.Scanner(System.in);

    //Uncomment each simple example below to run it.
/*
    //
    // 1) An echo game: take whatever the user types and print it back to them.
    //    Keep doing this until they enter nothing.
    //
    // This solution initializes input in a way to ensure the loop always
    // starts the first time.
    //
    String input = "something";   //so that we enter the loop the first time
    while (!input.equals(""))  {  //or use length or isEmpty methods
      System.out.print("Enter something: ");
      input = keybd.nextLine();
      System.out.println(input);
    }
    //This version does print the empty string out, though we could avoid this
    //with an if around the println.
*/
/*
    //
    // 1b) Sometimes it's simpler just to use a boolean variable to control
    // your while loop
    //
    boolean keepAsking = true;
    while (keepAsking)  {
      System.out.print("Enter something: ");
      String input = keybd.nextLine();
      if (input.length() == 0) {
        keepAsking = false;
      }else {
        System.out.println(input);
      }
    }
    //This version stops printing as soon as the user enters nothing.
    //Note a different way to recognizing an empty String
    //(.length() rather than .equals)
*/
/*
    //
    // 1c) This way works too, but requires duplicating most of the
    // while loop's body before the while loop.  This is not a problem
    // with only a couple lines, but it is bad practice if you start
    // duplicating more than that.
    //
    System.out.print("Enter something: ");
    String input = keybd.nextLine();
    while (input.length() > 0) {
      System.out.println("You said: " + input);
      System.out.print("Enter something: ");
      input = keybd.nextLine();
    }
*/
/*
    //
    // 1d) A do-while loop would also work well for this problem.
    //
    String input;
    do {
      System.out.print("Enter something: ");
      input = keybd.nextLine();
      if (input.length() > 0) {
        System.out.println("You said: " + input);
      }
    }while (input.length() > 0);
*/
/*
    //
    // 1e) You can also use an infinite loop and then a break statement
    // to end it.  Some people claim this is bad practice because you
    // have to hunt through the body of the loop to find out how it
    // will end.  (This can be a complicated task if the body of the
    // loop is long.)  However, you may run into this idiom occasionally,
    // and it also shows how to use break to end an loop early.
    //
    while (true) {
      System.out.print("Enter something: ");
      String input = keybd.nextLine();
      if (input.length() == 0) {
        break;
      }
      System.out.println("You said: " + input);
    }
*/



/*
    //
    // 2) Print out all the words to the song "99 Bottles of Beer"
    //
    for (int x = 99; x > 0; x--) {
      System.out.println(x + " bottles of beer on the wall, ");
      System.out.println(x + " bottles of beer.");
      System.out.println("Take one down, pass it around:");
      System.out.println((x - 1) + " bottles of beer on the wall.");
      System.out.println();
    }
    // Note how we have to use (x - 1), rather than x--, to avoid decrementing
    // x twice each time through the loop.
*/
/*
    //
    // 2b) Using a while loop and the prefix decrement operator
    // to good effect.
    //
    int beers = 99;
    while (beers > 0) {
      System.out.println(beers + " bottles of beer on the wall");
      System.out.println(beers + " bottles of beer");
      System.out.println("Take one down, pass it around");
      System.out.println(--beers + " bottles of beer on the wall.");
      System.out.println();
    }
*/



/*
    //
    // 3) Print all the powers of 2 that are less than 5000.
    // So, we want: 2, 4, 8, 16, 32, 64, ... 4096.
    //
    for (int exp = 1; Math.pow(2, exp) < 5000; exp++) {
      System.out.println("2^" + exp + " = " + (int) Math.pow(2, exp));
    }
*/
/*
    //
    // 3b) Here, "power" is the current power of 2.  Each time through the
    // loop, it gets * 2, to give the next power.  The exponent is just
    // tracked for output purposes.
    //
    int power = 2;
    int exponent = 1;  //optional; we really only need power
    while (power < 5000) {
      System.out.println("2^" + exponent + " = " + power);
      power *= 2;
      exponent++;
    }
*/
/*
    //
    // 3c) If don't care to track the exponent, 3b could be done instead
    //     as a simple for loop.  This is an example of how a loop counter
    //     can be altered in other ways than a simple increment or decrement.
    //
    for (int power = 2; power < 5000; power *= 2) {
      System.out.println(power);
    }
*/
/*
    //
    // 3d) In fact, we could even do the full version of 3b as a for loop.
    // However, this is quite complex and so the first form, or some mix,
    // would probably be clearer.
    //
    for (int power = 2, exponent = 1; power < 5000; power *= 2, exponent++) {
      System.out.println("2^" + exponent + " = " + power);
    }
*/



/*
    //
    // 4) Print backwards all the positive even integers starting with 100.
    // (That is: 100, 98, 96, 94.... 2.)
    //
    for (int i = 100; i > 0; i--) {
      //go backwards through all positive ints from 100 through 1
      //printing only the even ones
      if (i % 2 == 0) {
        System.out.println(i);
      }
    }
*/
/*
    //
    //4b) Or, we could just hit only the even numbers using the for loop.
    //    Note, too, a different ending condition than 4a.
    //
    for (int i = 100; i >= 2; i -= 2) {
      System.out.println(i);
    }
*/



/*
    //
    // 5) Print the numbers 1 through 10 on one line, with a comma after
    // each except the last one.  So, the list should be look exactly like:
    // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
    //
    int i; //declare i out here so we can use it after the loop
    for (i = 1; i < 10; i++) {
      System.out.print(i + ", ");
    }
    System.out.println(i + ".");
*/
/*
    //
    // 5b) Or, if we wanted to keep i within the loop...
    //
    for (int i = 1; i <= 10; i++) {
      System.out.print(i);
      if (i < 10) {
        System.out.print(", ");
      }
    }
    System.out.println(".");
*/



/*
    //
    // 6) Hi-lo guessing game.  The computer picks an int between 1 and 100
    // and then tells the user whether each guess is high or low until
    // they guess the number correctly.
    //
    // The try/catch is optional.  But, if included, you must clear the
    // String out of the buffer, or else the Scanner just tries to read it in
    // again next time through the loop, throwing the same exception again.
    //
    int target = (int) (Math.random() * 100) + 1;  //1 to 100
    System.out.println("Try to guess what number I have in mind (1 to 100).");
    int guess = 0;

    while (guess != target) {
      try {
        System.out.print("Enter your guess: ");
        guess = keybd.nextInt();
        if (guess < target) {
          System.out.println("Too low.");
        }else if (guess > target) {
          System.out.println("Too high.");
        }
      }catch (java.util.InputMismatchException ime) {
        System.out.println("Sorry, you must enter an integer.");
        keybd.nextLine(); //clear input stream
      }
    }
    //can only get here when loop ends: guess == target
    System.out.println("You guessed it!");
*/

  }
}

