public class LoopDemo { public static void main(String[] args) { //displayBlastoff(); //displayGrid(); //displayTriangle(); //displayNumTriangle(); //displayNumSequence(); displayDotsNums(); } // display the following text using a loop: // T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! // Note that loop bounds can be any expression. public static void displayBlastoff() { System.out.print("T-minus "); for(int i = 10; i >= 1; i++) { System.out.print(i + ", "); } System.out.print("blastoff!"); } /* display the following grid of stars using for loops: ********** ********** ********** ********** ********** */ public static void displayGrid() { for(int i = 1; i <= 5; i++) { for(int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); } } /* * ** *** **** ***** */ public static void displayTriangle() { for(int i = 1; i <= 5; i++) { for(int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } /* 1 22 333 4444 55555 */ public static void displayNumTriangle() { for(int i = 1; i <= 5; i++) { for(int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } } /* Mapping loop counter to number sequences 17 13 9 5 1 count number count * -4 count * -4 + 21 1 17 -4 17 2 13 -8 13 3 9 -12 9 4 5 -16 5 5 1 -20 1 */ public static void displayNumSequence() { for(int count = 1; count <= 5; count++) { System.out.print(count * -4 + 21 + " "); } } /* Mapping complex patterns to loop counters ....1 ...2 ..3 .4 5 for each line: print dots, print a number line dots number line * -1 line * -1 + 5 1 4 1 -1 4 2 3 2 -2 3 3 2 3 -3 2 4 1 4 -4 1 5 0 5 -5 0 How many dots on each line? line * -1 + 5 dots */ public static void displayDotsNums() { for(int line = 1; line <= 5; line++) { for(int j = 1; j <= line * -1 + 5; j++) { System.out.print("."); } System.out.println(line); } } }