// Helene Martin, CSE 142 // Demonstrates usage of for loops and nested for loops public class LoopExamples { public static void main(String[] args) { squares(); blastoff(); square(); mult(); } public static void squares() { for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + i * i); } /* 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); */ } // 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 square() { for (int i = 1; i <= 5; i++) { // System.out.println("**********"); // The following for loop and blank println // are equivalent to the statement above. for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); } } // display a multiplication table public static void mult() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i * j + "\t"); } System.out.println(); } } }