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