CS 46A - Lecture 14

Cover page image

Pre-class reading

for Loops

Syntax 6.2 The for Statement

The for Statement

for Loop Flowchart

Figure 3 - Flowchart of a for Loop

Execution of a for Loop

Figure 4 - Execution of a for Loop

ch06/invest2/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;
      }
   }

   /**
      Keeps accumulating interest for a given number of years.
      @param numberOfYears the number of years to wait
   */
   public void waitYears(int numberOfYears)
   {
      for (int i = 1; i <= numberOfYears; i++)
      {
         double interest = balance * rate / 100;
         balance = balance + interest;
      }
      years = years + n;
   }

   /**
      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/invest2/InvestmentRunner.java

/**
   This program computes how much an investment grows in
   a given number of years.
*/
public class InvestmentRunner
{
   public static void main(String[] args)
   {
      final double INITIAL_BALANCE = 10000;
      final double RATE = 5;
      final int YEARS = 20;
      Investment invest = new Investment(INITIAL_BALANCE, RATE);
      invest.waitYears(YEARS);
      double balance = invest.getBalance();
      System.out.printf("The balance after %d years is %.2f\n", 
            YEARS, balance);
   }   
}

Program Run:

Lecture 14 Clicker Question 1

Which of the following is true?

  1. Every for loop can be rewritten as a while loop, and every while loop can be rewritten as a for loop
  2. Some for loops can be rewritten as while loops, and all while loops can be rewritten as for loops
  3. All for loops can be rewritten as while loops, and some while loops can be rewritten as for loops
  4. There are for loops that can't be rewritten as while loops, and there are while loops that can't be rewritten as for loops

Lecture 14 Clicker Question 2

How many times does the following for loop execute?
int sum = 0;
for (i = 0; i <= 10; i++)
   sum = sum + i;
  1. 9 times
  2. 10 times
  3. 11 times
  4. 55 times

for Loop Examples

for Loop Examples

Symmetric Bounds

Asymmetric Bounds

Traversing Strings

Common Errors: Semicolons

Lecture 14 Clicker Question 3

How many times is the condition of this for loop evaluated?

for (int i = 1; i <= 6; i = i + 2)

Lecture 14 Clicker Question 4

What does this loop do?

int result = 0;
for (int i = 0; i <= str.length(); i++)
{
   char ch = str.charAt(i);
   if (Character.isDigit(ch))
      result = result + ch;
}
  1. It extracts all digits from a string, e.g. "He110!" -> "110"
  2. It sums all digits from a string, e.g. "He110!" -> 3
  3. It extracts the integer inside a string, e.g. "He110!" -> 110
  4. Something else

Loop Practice

CodeCheck (experimental; follow the “Code Completion” link)

PhoneNumbers