Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slides by Evan Gallagher

Similar presentations


Presentation on theme: "Slides by Evan Gallagher"— Presentation transcript:

1 Slides by Evan Gallagher
For Loops October 16, 2013 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher

2 To implement while, for and do loops
Goals To implement while, for and do loops To avoid infinite loops and off-by-one errors Know the syntax of a for loop and how it works. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

3 The for Loop To execute statements a certain number of times
“You “simply” take 4,522 steps!!! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

4 The for Loop Often you will need to execute a sequence of statements a given number of times. You could use a while loop for this. counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { System.out.println(counter); counter++; // Update the counter } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

5 Java has a statement custom made for this sort of processing:
The for Loop Java has a statement custom made for this sort of processing: the for loop. for (counter = 1; counter <= 10; counter++) { System.out.println(counter); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

6 initialization condition statements update
The for Loop Is Better than while for Doing Certain Things Doing something a certain number of times or causing a variable to take on a sequence of values is so common, Java has a statement just for that: for (int count = 1; count <= 10; count++) { System.out.println(count); } initialization condition statements update C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

7 for (initialization; condition; update) { statements }
The for Loop for (initialization; condition; update) { statements } The initialization is code that happens once, before the check is made, in order to set up for counting how many times the statements will happen. The loop variable is created here. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

8 (covered before break.)
The for Loop (covered before break.) for (initialization; condition; update) { statements } The condition is code that tests to see if the loop is done. When this test is false, the for statement is over and we go on to the next statement. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

9 (covered before break.)
The for Loop (covered before break.) for (initialization; condition; update) { statements } The statements are repeatedly executed - until the condition is false. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

10 (covered before break.)
The for Loop (covered before break.) for (initialization; condition; update) { statements } The update is code that causes the condition to eventually become false. Usually it’s incrementing or decrementing the loop variable. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

11 The for Statement The variable i is defined only in this for loop.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

12 Solving a Problem with a for Statement
Now let’s see the interest in action: We want to print the balance of our savings account over a five-year period. The “…over a five-year period” indicates that a for loop should be used. Because we know how many times the statements must be executed, we choose a for loop. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

13 The output should look something like this:
Solving a Problem with a for Statement The output should look something like this: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

14 Solving a Problem with a for Statement
The pseudocode: for (int year = 1; year <= nyears; year++) { Update balance. Print year and balance. } The algorithm C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

15 They are: Solving a Problem with a for Statement
Two statements should happen five times. So use a for statement. They are: update balance print year and balance for (int year = 1; year <= nyears; year++) { } // update balance // print year and balance C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

16 The Modified Investment Program Using a for Loop
public static void main(String [] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; double balance = INITIAL_BALANCE; Scanner scan = new Scanner(System.in); System.out.println(“Number of Years?”); int nyears =scan.nextInt(); DecimalFormat f = new DecimalFormat(“$0.00”); for (int year = 1; year <= nyears; year++) balance = balance * (1 + RATE / 100); System.out.println(year + “ “ + f.format(balance)); } C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

17 The Modified Investment Program Using a for Loop
A run of the program: Enter number of years: 10 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

18 Example of Normal Execution – Taking Bigger Steps
for loop to hand-trace What is the output? for (int i = 0; i < 9; i += 2) cout << i << " "; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

19 for(int n = 1; n<= 5; n++) { s = s + n; System.out.println(s); }
R4.14a Trace this. int s = 1; for(int n = 1; n<= 5; n++) { s = s + n; System.out.println(s); }

20 Write a Program Write a program to output “Winter Wonderland." seven times. Use a for loop to repeat the output of the line, “Winter Wonderland.”


Download ppt "Slides by Evan Gallagher"

Similar presentations


Ads by Google