package chapter.pkg6; public class Chapter6 { // EXAMPLE OF A WHILE LOOP public static void calculate() { double balance = 1000; double goal = 2000; int years = 0; double interestRate = 0.03; while (balance < goal) { balance = balance + balance * interestRate; // shorthand: balance += balance * interestRate; years++; System.out.println("year " + years + " balance: $" + balance); } System.out.println("year " + years + " balance: $" + balance); } // FOR LOOP EXAMPLE public static void firstForLoop() { int k; for (k = 0; k < 10; k++) { System.out.println("Happy Thursday!"); } } // EXAMPLE OF NESTED LOOPS public static void nestedLoop1() { int i, j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 4; j++) System.out.print("*"); System.out.println(); } } // ANOTHER EXAMPLE OF NESTED LOOPS public static void nestedLoop2() { int i, j; for (i = 1; i <= 6; i++) { for (j = 1; j <= 5; j++) { if ((i + j) % 2 == 0) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } public static void main(String[] args) { Chapter6.calculate(); Chapter6.firstForLoop(); Chapter6.nestedLoop2(); } }