class Example { public static void main(String[] args) { System.out.println("This program will give you several ways to do the same program"); System.out.println("Stars"); /* 1. ================================================================ */ //First one System.out.println("1."); System.out.println("First one as no loop:\n"); System.out.println("*****"); System.out.println("****"); System.out.println("***"); System.out.println("**"); System.out.println("*"); /* 2. ================================================================ */ System.out.println("2."); //Second one is a classical for loop. Fastest. System.out.println("Second one with 2 for loops:\n"); //Define the integer for the count in the for loop and the break point for (int count = 5; count >= 1; count-- ) { //define another count variable to print the stars for (int starCount = 1; starCount <= count; starCount++ )//this is fastest way { System.out.print("*"); } System.out.println(); } /* 3. ================================================================ */ System.out.println("3."); System.out.println("Third one with a while and a for loop:\n"); //I personally do not recommend usage of while loop if you are doing //a count operation. Here, on this example we do counting (of the stars) //so one should use the for loop. //Define the integer for the count in the for while loop int count3 = 5; while (count3 != 0) { //This part is the same as the one at the nested for loops' inner for loop. //We can do this part with while also, not recommended. for (int starCount = 1; starCount <= count3; starCount++ )//this is fastest way { System.out.print("*"); } System.out.println(); count3--; } /* 4. ================================================================ */ System.out.println("4."); //Let's do something different. I will not use any count variables. (how?) //Let's define the string as "*****". System.out.println("Fourth one, very wierd!\n"); String str = "*****"; for (; str.length() > 0;str = str.substring(0, str.length()-1)) { System.out.println(str); } /* 5. ================================================================ */ System.out.println("5."); //Lets count from 1 to 5 //The usual and fast and readable way is: System.out.println("Let's Count:\nFrom one to five\n"); for (int i = 1; i<=5; i++ ) { System.out.println(i); } /* 6. ================================================================ */ System.out.println("6."); //The while version which I don't recommend. System.out.println("From one to five, while version 1\n"); int count6 = 1; while (count6 <=5 ) { System.out.println(count6); count6++; } System.out.println("From one to five, while version 2\n"); /* 7. ================================================================ */ System.out.println("7."); int count7 = 1; while (count7 <=5 ) { System.out.println(count7++); } /* 8. ================================================================ */ System.out.println("8."); System.out.println("From one to five, for loop like while loop \n"); int count8 = 1; for (; ; ) { System.out.println(count8++); if (count8 == 6) { break;//break breaks any loop that it is inside } } //use for loop like while loop // /* 9. ================================================================ */ System.out.println("9. ================================================================"); System.out.println("do while loop \n"); int counter9 = 1; do { System.out.println(counter9); counter9++; } while (counter9 <= 5); /* 10. ================================================================ */ System.out.println("10. ================================================================"); System.out.println("A special case: do-while loop control is set to false but it prints.\n"); do { System.out.println("This will not be printed on regular while loop\nBut do while prints"); } while (false); /* 11. ================================================================ */ System.out.println("11. ================================================================"); System.out.println("Count variable after for loop.\n"); int counter11 = 0; System.out.println("before for. counter11: "+counter11); for (; counter11<=5 ;counter11++ ) { System.out.println("inside for. counter11: "+counter11); } System.out.println("after for. counter11: "+counter11); } }