// Yazzy Latif // 06/29/2020 // TA: Grace Hopper // For Loops Practice // This program contains a number of short methods, all of which use for loops. /* A few different general for loop patterns - note that each of these run 10 times. for (int i = 1; i <= 10; i++) { body } for (int i = 0; i < 10; i++) { body } for (int i = 10; i >= 1; i--) { body } */ public class ForLoops { public static void main(String[] args) { printSquares(); System.out.println(); count(); System.out.println(); blastoff(); System.out.println(); square(); System.out.println(); numberPyramid(); System.out.println(); lineNumberPyramid(); System.out.println(); complexLine(); } /* display the following output: 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 5 squared = 25 6 squared = 36 Done! */ public static void printSquares() { for (int i = 1; // init -> 1 i <= 6; // test -> 2 i++) { // update -> 3 System.out.println(i + " squared = " + (i * i)); // body -> 4 } System.out.println("Done!"); // after -> 5 } // display the following output: // 1 2 3 4 5 6 7 8 9 10 public static void count() { for (int i = 1; i <= 10; i++) { System.out.print(i + " "); } System.out.println(); } // What does the following method output? public static void blastoff() { System.out.print("T-minus "); for (int i = 10; i >= 1; i--) { System.out.print(i + ", "); } System.out.println("blastoff!"); } /* display the following square of stars: ********** ********** ********** ********** ********** */ public static void square() { // outer loop repeats over line for (int line = 1; line <= 5; line++) { // inner loop controls in line repetition for (int stars = 1; stars <= 10; stars++) { System.out.print("*"); } System.out.println(); } } // What is the output of the following method? public static void numberPyramid() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } /* display a pyramid of line numbers 1 22 333 4444 55555 */ public static void lineNumberPyramid() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } } /* display the following output ....1 ...2 ..3 .4 5 */ public static void complexLine() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= -1 * i + 5; j++) { System.out.print("."); } System.out.println(i); } } }