CS 46A - Lecture 13

Cover page image

Pre-class reading

while Loops

Calculating the Growth of an Investment

Figure 1 - Execution of a while Loop

ch06/invest1/Investment.java

/**
   A class to monitor the growth of an investment that 
   accumulates interest at a fixed annual rate.
*/
public class Investment
{
   private double balance;
   private double rate;
   private int years;

   /**
      Constructs an Investment object from a starting balance and
      interest rate.
      @param aBalance the starting balance
      @param aRate the interest rate in percent
   */
   public Investment(double aBalance, double aRate)
   {
      balance = aBalance;
      rate = aRate;
      years = 0;
   }

   /**
      Keeps accumulating interest until a target balance has
      been reached.
      @param targetBalance the desired balance
   */
   public void waitForBalance(double targetBalance)
   {
      while (balance < targetBalance)
      {
         years++;   
         double interest = balance * rate / 100;
         balance = balance + interest;
      }
   }

   /**
      Gets the current investment balance.
      @return the current balance
   */
   public double getBalance()
   {
      return balance;
   }

   /**
      Gets the number of years this investment has accumulated
      interest.
      @return the number of years since the start of the investment
   */
   public int getYears()
   {
      return years;
   }
}

ch06/invest1/InvestmentRunner.java

/**
   This program computes how long it takes for an investment
   to double.
*/
public class InvestmentRunner
{
   public static void main(String[] args)
   {
      final double INITIAL_BALANCE = 10000;
      final double RATE = 5;
      Investment invest = new Investment(INITIAL_BALANCE, RATE);
      invest.waitForBalance(2 * INITIAL_BALANCE);
      int years = invest.getYears();
      System.out.println("The investment doubled after "
            + years + " years");
   }   
}

Program Run:

while Loop Flowchart

Figure 2 - Flowchart of a while Loop

while Loop Examples

while Loop Examples

Syntax 6.1 The while Statement

The while Statement

Lecture 13 Clicker Question 1

What does the following code segment print?
while (false) print("Hello");
println(" World");

  1. Hello World
  2. HelloHelloHello... (and never stops)
  3. World
  4. Nothing—the code segment has a syntax error

Lecture 13 Clicker Question 2

What would happen if the constant RATE was set to 0 in the main method of the InvestmentRunner program?

  1. The waitForBalance method would return immediately, without entering the loop
  2. The waitForBalance method would enter the loop once and then return
  3. The waitForBalance method would never return due to an infinite loop.
  4. The waitForBalance method would throw an exception

Common Error: Infinite Loops

Common Error: Off-by-One Errors

Avoiding Off-by-One Error

Tracing a Loop

Loop Tracing Example

Lecture 13 Clicker Question 3

What does this loop print?

int n = 1729;
String result = "";
while (n > 0)
{
   int digit = n % 10;
   result = result + digit;
   n = n / 10;
}
System.out.println(result);
  1. 19
  2. 1729
  3. 9271
  4. Nothing--the code has a syntax error

Lecture 13 Clicker Question 4

What does this loop print?

String greeting = "Hello";
String result = "";
int i = greeting.length() - 1;
while (i > 0)
{
   String letter = greeting.substring(i, i + 1);
   result = result + letter;
   i--;
}
System.out.println(result);
  1. olleH
  2. olle
  3. lleH
  4. Nothing—an exception is thrown

Loop Practice