Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop.

Similar presentations


Presentation on theme: "6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop."— Presentation transcript:

1 6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop when a variable runs from a starting value to an ending value with a constant increment or decrement Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

2 Syntax 6.2 The for Statement
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

3 for Loop Flowchart Big Java by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved.

4 Execution of a for Loop Big Java by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved.

5 for Loops /** * A method to monitor the growth of an investment that
* accumulates interest at a fixed annual rate for a given number of years * @param startBalance the starting balance * @param rate the interest rate in percent * @param years the number of years it grows * @return the balance */ public static double calcBalance (double startBalance, double rate, int years){ double balance = startBalance; for (int i = 1; i <= years; i++) { double interest = balance * rate / 100; balance = balance + interest; } return balance } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

6 for Loops Program Run: The balance after 20 years is 26532.98 1 /**
1 /** 2 This program computes how much an investment grows in 3 a given number of years. 4 */ 5 public class InvestmentRunner { 6 public static void main(String[] args) { final double INITIAL_BALANCE = 10000; final double RATE = 5; final int YEARS = 20; 10 double balance = caluBalance(INITIAL_BALANCE, RATE, YEARS); System.out.printf("The balance after %d years is %.2f\n", YEARS, balance); } 15 } Program Run: The balance after 20 years is Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

7 Rewrite the for loop in the calcBalance method as a while loop.
Self Check 6.3 Rewrite the for loop in the calcBalance method as a while loop. Answer: int i = 1; while ( i <= years ) { double interest = balance * rate / 100; balance = balance + interest; i++; } for (int i = 1; i <= years; i++) { double interest = balance * rate / 100; balance = balance + interest; } Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

8 How many times does the following for loop execute?
Self Check 6.4 How many times does the following for loop execute? for (i = 0; i <= 10; i++) System.out.println(i * i); Answer: 11 times. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

9 for Loop Examples Big Java by Cay Horstmann
Copyright © 2009 by John Wiley & Sons. All rights reserved.

10 Common Errors: Semicolons
A semicolon that shouldn’t be there: sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System.out.println(sum); Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.


Download ppt "6.2 for Loops Example: for ( int i = 1; i <= 10; i++ ) { double interest = balance * rate / 100; balance = balance + interest; } Use a for loop."

Similar presentations


Ads by Google