/* Loops4.java CIS 160 David Klick 2004-09-17 Demonstrates repetition and accumulators */ public class Loops4 { public static void main(String[] args) { int i; int accum; System.out.println("This program adds up the even numbers" + " from 1 to 10 using different types of loops."); System.out.print("\nA do loop: "); i = 2; accum = 0; do { accum = accum + i; i = i + 2; } while (i <= 10); System.out.print(accum); // this is called a pre-test loop because the test // is at the beginning of the loop System.out.print("\nA while loop: "); i = 2; accum = 0; while (i <= 10) { accum = accum + i; i = i + 2; } System.out.print(accum); // the for loop is also a pre-test loop System.out.print("\nA for loop: "); accum = 0; for (i=2; i<=10; i=i+2) { accum = accum + i; } System.out.print(accum); } }