public class LoopFirstExamples { public static void main(String[] args) { // Print a table of the numbers from 1 to 100 int i = 1; while (i <= 100) { if (i % 10 != 0) { System.out.print(i + ", "); } else { System.out.println(i); } i++; // means i = i + 1 } // Print the sum 1 + 2 + 3 + 4 + ... + 100 (should be 5050) // with a while loop int sum = 0; i = 1; while (i <= 100) { sum += i; i++; } System.out.println("sum with a while loop = " + sum); // with a do-while loop sum = 0; i = 1; do { sum += i; i++; } while (i <= 100); System.out.println("sum with a do-while loop = " + sum); // with a for loop sum = 0; // int k; k can be used outside of the for loop for (int k = 1; k <= 100; k++) { // or just within the for loop if // declared in the for statement sum += k; } System.out.println("sum with a for loop = " + sum); // System.out.println("k = " + k); } }