// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 7 - Loops // Program to demonstrate for loop // ForLoopDemo.java 1 JAN 05 public class ForLoopDemo { 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 balance float annualDeposit = 100F; // then deposit $100 at end of each year float apr = 0.05F; // annual interest rate 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 for Loop"); // print headings System.out.println ("Year Balance"); // beginning of for loop for (numberOfYears = 0; currentBalance <= 2 * initialBalance; numberOfYears = numberOfYears + 1) { currentBalance = currentBalance * (1 + apr) + annualDeposit; System.out.println (numberOfYears + " " + Math.round(currentBalance)); } // end of for loop System.out.println ("It will take " + numberOfYears + " years."); System.out.println ("The balance will be " + Math.round(currentBalance)); } // end of main method } // end of ForLoopDemo.java class program