// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 7 - Loops // Program to demonstrate do loop // DoLoopDemo.java 1 JAN 05 public class DoLoopDemo { public static void main (String args[]) { // we have an amount of money in an interest bearing account: initialBalance // each year we make a fixed deposit to the account: annualDeposit // the account earns at an annual rate of interest: apr // how many years are required for our account to double in value: years // declare variables float initialBalance = 1000F; // let's begin with $1,000 float annualDeposit = 100F; // then deposit $100 at end of each year float apr = 0.05F; // apr is 5% float currentBalance; // running balance int numberOfYears; // count the number of years // initialize currentBalance and numberOfYears currentBalance = initialBalance; numberOfYears = 0; System.out.println ("Output From do Loop"); // print headings System.out.println ("Year Balance"); // beginning of do loop do { currentBalance = currentBalance * (1 + apr) + annualDeposit; numberOfYears = numberOfYears + 1; // increment numberOfYears System.out.println (numberOfYears + " " + Math.round(currentBalance)); } while (currentBalance <= 2 * initialBalance); // end of do loop System.out.print ("In " + numberOfYears + " years."); System.out.println ("The balance will be " + Math.round(currentBalance)); } // end of main method } // end of DoLoopDemo.java