
public class ForLoopExamples {

	public static void main(String[] args) {
		//blueprints();
		demo1();
		demo2();
		demo3(); // nested loops
		demo4(); // scope example
		inClassPractice1();
	}

	public static void blueprints() {

		// For Loop Blueprint
		int size = 10;
		for (int i = 0; i < size; i++) {
			System.out.println("Loop # = " + (i + 1) + ", i = " + i);
		}

		// Counting Loop Blueprint
		for (int i = 0; i < 4; i++) {
			// statements
		}
	}

	public static void demo1() {

		// Without loops, 1-6
		System.out.println("DEMO1 without loop");
		System.out.println("The numbers 1 - 6: ");
		System.out.println("1");
		System.out.println("2");
		System.out.println("3");
		System.out.println("4");
		System.out.println("5");
		System.out.println("6");

		// With loops, 1 - 500 or other
		System.out.println("DEMO1 with loop");
		System.out.println("The numbers 1 - 500: ");
		// 1. Write for loop
		// 2. Experiment with start, stop and step and infinite loop

	}

	public static void demo2() {

		// DEMO2 #1
		System.out.println("DEMO2 #1");
		for (int i = 1; i <= 10; i += 2) {
			System.out.println("The square of " + i + " is " + (i * i) + ".");
		}
		System.out.println();

		// DEMO2 #2 
		// Write code to calculate and print the factorial of the variable num.
		// Assume num >= 2.
		// 4! = 1 * 2 * 3 * 4
		System.out.println("DEMO2 #2");
		int num = 4;
		// ADD CODE HERE

		/*
		int factorial = 1;
		for (int i = 2; i <= num; i++) {
			factorial *= i;
		}
		System.out.println("The factorial of " + num + " is " + factorial + ".");
		*/

		// DEMO2 #3
		System.out.println("DEMO2 #3");
		// Write code to add the sum of all positive integers up to and 
		// and including a given number, num. Fix the errors.
		/* int sum = 0;
		for (int i = 0; i < num, i++);
		{
			sum += 1;
		}
		System.out.println("The sum is " + sum + ".");
		*/
	}

	public static void demo3() {

		// #1
		// 1. This prints one line, add other lines
		System.out.println("DEMO3");
		for (int i = 0; i < 10; i++) {
			System.out.print(i + " ");
		}

		/* ANSWER
				for (int i = 0; i < 10; i++) {
			for (int j = 1; j <= 10; j++) {
				System.out.print(i * 10 + j + " ");
			}
			System.out.println();
		}
		*/
	}

	public static void demo4() {
		for (int i = 0; i < 10; i++) { // i created 
			System.out.print(i + " ");
		} // i stops existing
		System.out.println();
		for (int i = 500; i < 10000; i++) { // new i created
			System.out.print(i + " ");
		} // i stops existing
	}

	public static void inClassPractice1() {

		// #1
		System.out.println("IN CLASS PRACTICE #1");
		int x = 11;
		for (int i = x / 2; i >= -2; i--) {
			System.out.print("*");
			System.out.print("**");
		}
		System.out.print("\n\n");

		// #2
		System.out.println("IN CLASS PRACTICE #2");
		int factorial = 1;
		int num = 4;
		for (int i = 1; i <= num; i++) {
			factorial *= i;
		}
		System.out.println(num + "! = " + factorial);

		// #3
		System.out.println("IN CLASS PRACTICE #3");
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(i);
			}
			System.out.println();
		}
		System.out.println();
	}

	public static void extra() {

		// Extra - Newton's Square Root Approximator
		int number = 9;
		double approx;
		approx = number / 2;
		for (int i = 0; i < 7; i++) {
			approx = ((number / approx) + approx) / 2;
			System.out.print("Approximation #" + (i + 1) + ": " + approx);
			System.out.println(" squared is " + approx * approx);
		}

		// 2.3 Nested For Loops
		// It's a mystery!
		int numRows = 5, numCols = 3;
		for (int i = 0; i < numRows; i++) {
			for (int j = 0; j < numCols; j++) {
				System.out.print("R" + (i + 1) + "C" + (j + 1) + " ");
			}
			System.out.println();
		}
		System.out.println();

		// Practice #4

	}

}
