// Helene Martin, CSE 142 // Several for loop examples public class ForLoops { public static void main(String[] args) { // demonstrates basic for loop syntax. Put a stop sign // on this line and run with debugger to see how for // loops are executed for (int i = 1; i <= 7; i+=2) { System.out.println("Hi"); } squares(); lantern(); temps(); blastoff(); grid(); mult(); triangle(); } // prints the first 12 squares public static void squares() { for (int i = 1; i <= 12; i++) { System.out.println(i + " squared = " + i * i); } /*System.out.println("1 squared = " + 1 * 1); System.out.println("2 squared = " + 2 * 2); System.out.println("3 squared = " + 3 * 3); System.out.println("4 squared = " + 4 * 4); System.out.println("5 squared = " + 5 * 5); System.out.println("6 squared = " + 6 * 6);*/ } // displays a paper lantern image public static void lantern() { System.out.println("+----+"); for (int i = 1; i <= 2; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+"); } // converts degrees centigrade to farenheight public static void temps() { int highTemp = 5; for (int i = -3; i <= highTemp / 2; i++) { System.out.print(i * 1.8 + 32 + " "); } System.out.println(); } // display the following text using a loop: // T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! 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 grid of stars using for loops: ********** ********** ********** ********** ********** */ public static void grid() { for (int row = 1; row <= 5; row++) { for (int col = 1; col <= 10; col++) { System.out.print("*"); } System.out.println(); } } // displays a multiplication table public static void mult() { for (int row = 1; row <= 5; row++) { for (int col = 1; col <= 10; col++) { System.out.print(row * col + "\t); } System.out.println(); } } }