Chapter 6 Iteration Chapter 6 Iteration 1 Chapter
- Slides: 74
Chapter 6 Iteration Chapter 6 Iteration 1
Chapter Goals q To be able to program loops with while and for (sometimes do) statements q To avoid infinite loops and off-by-one errors q To understand nested loops q To learn how to process input q To implement simulations Chapter 6 Iteration 2
while Loops q q Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; q Usually, the statement is a block statement (set of statements enclosed in { }) Chapter 6 Iteration 3
Calculating the Growth of an Investment q Invest $10, 000, 5% interest, compounded annually Year 0 1 2 Balance $10, 000 $10, 500 $11, 025 3 4 5 $11, 576. 25 $12, 155. 06 $12, 762. 82 Chapter 6 Iteration 4
Calculating the Growth of an Investment q When has the bank account reached a particular balance? int year = 0; while (balance < target. Balance) { year++; double interest = balance * rate / 100; balance = balance + interest; } Chapter 6 Iteration 5
File Investment. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** A class to monitor the growth of an investment that accumulates interest at a fixed annual rate. */ public class Investment { /** Constructs an Investment object from a starting balance and interest rate. @param a. Balance the starting balance @param a. Rate the interest rate in percent */ public Investment(double a. Balance, double a. Rate) { balance = a. Balance; rate = a. Rate; years = 0; } 19: Chapter 6 Iteration Continued… 6
File Investment. java 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: /** Keeps accumulating interest until a target balance has been reached. @param target. Balance the desired balance */ public void wait. For. Balance(double target. Balance) { while (balance < target. Balance) { years++; double interest = balance * rate / 100; balance = balance + interest; } } /** Gets the current investment balance. @return the current balance */ Chapter 6 Iteration Continued… 7
File Investment. java 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: } public double get. Balance() { 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 get. Years() { return years; } private double balance; private double rate; private int years; Chapter 6 Iteration 8
File Investment. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: /** This program computes how long it takes for an investment to double. */ public class Investment. Tester { public static void main(String[] args) { final double INITIAL_BALANCE = 10000; final double RATE = 5; Investment invest = new Investment(INITIAL_BALANCE, RATE); invest. wait. For. Balance(2 * INITIAL_BALANCE); int years = invest. get. Years(); System. out. println("The investment doubled after " + years + " years"); } } Continued… Chapter 6 Iteration 9
File Investment. Tester. java Output The investment doubled after 15 years Chapter 6 Iteration 10
while Loop Flowchart Figure 1: Flowchart of a while Loop Chapter 6 Iteration 11
Syntax 7. 1: The while Statement while (condition) statement Example: while (balance < target. Balance) { year++; double interest = balance * rate / 100; balance = balance + interest; } Purpose: To repeatedly execute a statement as long as a condition is true Chapter 6 Iteration 12
Self Check 1. How often is the statement in the loop while (false) statement; executed? 2. What would happen if RATE was set to 0 in the main method of the Investment. Tester program? Chapter 6 Iteration 13
Answers 1. Never 2. The wait. For. Balance method would never return due to an infinite loop Chapter 6 Iteration 14
Common Error: Infinite Loop q . int years = 0; while (years < 20) { double interest = balance * rate / 100; balance = balance + interest; } q int years = 20; while (years > 0) { years++; // Oops, should have been years-double interest = balance * rate / 100; balance = balance + interest; } q Loops run forever (must kill the program) Chapter 6 Iteration 15
Common Error: Off-By-One Errors int years = 0; while (balance < 2 * initial. Balance) { years++; double interest = balance * rate / 100; balance = balance + interest; } System. out. println("The investment reached the target after " + years + " years. "); q Should years start at 0 or 1? q Should the test be < or <=? Chapter 6 Iteration 16
Avoiding Off-by-One Error q q Look at a scenario with simple values: initial balance: $100 interest RATE: 50% after year 1, the balance is $150 after year 2 it is $225, or over $200 so the investment doubled after 2 years the loop executed two times, incrementing years each time Therefore, years must start at 0, not at 1 Chapter 6 Iteration 17
Avoiding Off-by-One Error q Suppose interest rate is 100% q Then after one year § balance is 2 * initial. Balance so loop should stop q Therefore: must use < not <= q Think! § Don't just guess and compile Chapter 6 Iteration 18
do Loops q Executes loop body at least once: do statement while (condition); q Example: Validate input double value; do { System. out. print("Please enter a positive number: "); value = in. next. Double(); } while (value <= 0); Chapter 6 Iteration 19
do Loops q Can get same effect with a while loop boolean done = false; while (!done) { System. out. print("Please enter a positive number: "); value = in. next. Double(); if (value > 0) done = true; } Chapter 6 Iteration 20
do Loop Flowchart Figure 2: Flowchart of a do Loop Chapter 6 Iteration 21
Spaghetti Code q Code with confusing jumps is called spaghetti code q Hard to read q Hard to maintain q Avoid spaghetti code! Chapter 6 Iteration Figure 3: Spaghetti Code 22
for Loops q for (initialization; condition; update) statement Example: for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } Chapter 6 Iteration 23
for Loops q Equivalent to initialization; while (condition) { statement; update; } q Other examples for (years = n; years > 0; years--). . . for (x = -10; x <= 10; x = x + 0. 5). . . Chapter 6 Iteration 24
Flowchart for Loop Figure 4: Flowchart of a for Loop Chapter 6 Iteration 25
Syntax 7. 2: The for Statement for (initialization; condition; update) statement Example: for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } Purpose: To execute an initialization, then keep executing a statement and updating an expression while a condition is true Chapter 6 Iteration 26
File Investment. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** A class to monitor the growth of an investment that accumulates interest at a fixed annual rate */ public class Investment { /** Constructs an Investment object from a starting balance and interest rate. @param a. Balance the starting balance @param a. Rate the interest rate in percent */ public Investment(double a. Balance, double a. Rate) { balance = a. Balance; rate = a. Rate; years = 0; } Continued… Chapter 6 Iteration 27
File Investment. java 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: /** Keeps accumulating interest until a target balance has been reached. @param target. Balance the desired balance */ public void wait. For. Balance(double target. Balance) { while (balance < target. Balance) { years++; double interest = balance * rate / 100; balance = balance + interest; } } Continued… Chapter 6 Iteration 28
File Investment. java 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: /** Keeps accumulating interest for a number of years. @param n the number of years */ public void wait. Years(int n) { for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; } years = years + n; } /** Gets the current investment balance. @return the current balance */ Chapter 6 Iteration Continued… 29
File Investment. java 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: public double get. Balance() { 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 get. Years() { return years; } Chapter 6 Iteration Continued… 30
File Investment. java 68: 69: 70: 71: } private double balance; private double rate; private int years; Chapter 6 Iteration 31
File Investment. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** This program computes how much an investment grows in a given number of years. */ public class Investment. Tester { 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. wait. Years(YEARS); double balance = invest. get. Balance(); System. out. printf("The balance after %d years is %. 2 fn", YEARS, balance); } } Continued… Chapter 6 Iteration 32
File Investment. java Output The balance after 20 years is 26532. 98 Chapter 6 Iteration 33
Self Check 3. Rewrite the for loop in the wait. Years method as a while loop 4. How many times does the following for loop execute? for (i = 0; i <= 10; i++) System. out. println(i * i); Chapter 6 Iteration 34
Answers 3. 4. int i = 1; while (i <= n) { double interest = balance * rate / 100; balance = balance + interest; i++; } 11 times Chapter 6 Iteration 35
Common for Errors: Semicolons q A semicolon that should not be there sum = 0; for (i = 1; i <= 10; i++); sum = sum + i; System. out. println(sum); q A missing semicolon for (years = 1; (balance = balance + balance * rate / 100) < target. Balance; years++) System. out. println(years); Chapter 6 Iteration 36
Nested Loops q q Create triangle pattern [] [][][] Loop through rows: for each row for (int i = 1; i <= n; i++) { // make triangle row } Chapter 6 Iteration 37
Nested Loops q Another loop to make triangle row for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "n"; q A loop within a loop: nested loops for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "n"; } Chapter 6 Iteration 38
File Triangle. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: /** This class describes triangle objects that can be displayed as shapes like this: [] [][][] */ public class Triangle { /** Constructs a triangle. @param a. Width the number of [] in the last row of the triangle. */ public Triangle(int a. Width) { width = a. Width; } Continued… Chapter 6 Iteration 39
File Triangle. java 19: 20: 21: /** 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: } */ public String to. String() { String r = ""; for (int i = 1; i <= width; i++) { // Make triangle row for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "n"; } return r; } Computes a string representing the triangle. @return a string consisting of [] and newline characters private int width; Chapter 6 Iteration 40
File Triangle. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: /** This program tests the Triangle class. */ public class Triangle. Tester { public static void main(String[] args) { Triangle small = new Triangle(3); System. out. println(small. to. String()); Triangle large = new Triangle(15); System. out. println(large. to. String()); } } Chapter 6 Iteration 41
Self Check 5. How would you modify the nested loops so that you print a square instead of a triangle? 6. What is the value of n after the following nested loops? int n = 0; for (int i = 1; i <= 5; i++) for (int j = 0; j < i; j++) n = n + j; Chapter 6 Iteration 43
Answers 5. Change the inner loop to for (int j = 1; j <= width; j++) 6. 20 Chapter 6 Iteration 44
Avoid != in for Loops q Bad idea to use != to test for end of range for (i = 1; i != n; ++i). . . § What happens if n is negative? q Safer to use for (i = 1; i <= n; ++i). . . Chapter 6 Iteration 45
Scope of Variable in for Loop q Consider this… for (int i = 1; i <= n; ++i) {. . . } // i is no longer defined q Compared to this… int i; for (i = 1; i <= n; ++i) {. . . } // i is still defined here Chapter 6 Iteration 46
Use Loops as Intended q Java syntax lets you do weird things q Here’s one bad idea… q for (System. out. print(”Inputs: "); (x = in. Next. Double()) > 0; sum += x) ++count; And here’s another… for (int i = 1; i <= years; i++) { if (balance >= 5. 0) i = years - 3; . . . } Chapter 6 Iteration 47
Processing Sentinel Values q q Sentinel value: Can be used for indicating the end of a data set 0 or -1 make poor sentinels; better to use Q System. out. print("Enter value, Q to quit: "); String input = in. next(); if (input. equals. Ignore. Case("Q")) We are done else { double x = Double. parse. Double(input); . . . } Chapter 6 Iteration 48
Loop and a half q q Sometimes termination condition of a loop can only be evaluated in the middle of the loop Can use a boolean variable to control the loop: boolean done = false; while (!done) { Print prompt String input = read input; if (end of input indicated) done = true; else { // Process input } } Chapter 6 Iteration 49
File Input. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: import java. util. Scanner; /** This program computes the average and maximum of a set of input values. */ public class Input. Tester { public static void main(String[] args) { Scanner in = new Scanner(System. in); Data. Set data = new Data. Set(); boolean done = false; while (!done) { Chapter 6 Iteration Continued… 50
File Input. Tester. java 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: } System. out. print("Enter value, Q to quit: "); String input = in. next(); if (input. equals. Ignore. Case("Q")) done = true; else { double x = Double. parse. Double(input); data. add(x); } } System. out. println("Average = " + data. get. Average()); System. out. println("Maximum = " + data. get. Maximum()); } Chapter 6 Iteration 51
File Data. Set. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: /** Computes the average of a set of data values. */ public class Data. Set { /** Constructs an empty data set. */ public Data. Set() { sum = 0; count = 0; maximum = 0; } /** Adds a data value to the data set @param x a data value */ Chapter 6 Iteration Continued… 52
File Data. Set. java 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: public void add(double x) { sum = sum + x; if (count == 0 || maximum < x) maximum = x; count++; } /** Gets the average of the added data. @return the average or 0 if no data has been added */ public double get. Average() { if (count == 0) return 0; else return sum / count; } Chapter 6 Iteration Continued… 53
File Data. Set. java 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: } /** Gets the largest of the added data. @return the maximum or 0 if no data has been added */ public double get. Maximum() { return maximum; } private double sum; private double maximum; private int count; Chapter 6 Iteration 54
Output Enter value, Q Average = 7. 0 Maximum = 22. 0 Chapter 6 Iteration to to quit: 22 0 -1 Q 55
Self Check 7. Why does the Input. Tester class call in. next and not in. next. Double? 8. Would the Data. Set class still compute the correct maximum if you simplified the update of the maximum field in the add method to the following statement? if (maximum < x) maximum = x; Chapter 6 Iteration 56
Answers 7. Because we don't know whether the next input is a number or the letter Q 8. No. If all input values are negative, the maximum is also negative. However, the maximum field is initialized with 0. With this “simplification”, the maximum would be incorrectly computed as 0. Chapter 6 Iteration 57
break and continue Statements q Can use break to immediately exit the loop while (true) { String input = in. next(); if (input. equals. Ignore. Case(“Q”)) break; double x = Double. parse. Double(input); data. add(x); q }Also is a continue statement § Go immediately to next iteration of loop § Not as useful as break § Often use if/else instead of continue Chapter 6 Iteration 58
Random Numbers and Simulations q q In a simulation, you repeatedly generate random numbers and use them to simulate an activity Java random number generator: Random generator = new Random(); int n = generator. next. Int(a); // 0 <= n < a double x = generator. next. Double(); // 0 <= x < 1 q For example, to “throw” a die… int d = 1 + generator. next. Int(6); Chapter 6 Iteration 59
File Die. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: 18: import java. util. Random; /** This class models a die that, when cast, lands on a random face. */ public class Die { /** Constructs a die with a given number of sides. @param s the number of sides, e. g. 6 for a normal die */ public Die(int s) { sides = s; generator = new Random(); } Continued… Chapter 6 Iteration 60
File Die. java 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: } /** Simulates a throw of the die @return the face of the die */ public int cast() { return 1 + generator. next. Int(sides); } private Random generator; private int sides; Chapter 6 Iteration 61
File Die. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: 16: 17: /** This program simulates casting a die ten times. */ public class Die. Tester { public static void main(String[] args) { Die d = new Die(6); final int TRIES = 10; for (int i = 1; i <= TRIES; i++) { int n = d. cast(); System. out. print(n + " "); } System. out. println(); } } Chapter 6 Iteration 62
Output First Run 6 5 6 3 2 6 3 4 4 1 Second Run 3 2 2 1 6 5 3 4 1 2 Chapter 6 Iteration 63
Buffon Needle Experiment Figure 5: The Buffon Needle Experiment Chapter 6 Iteration 64
Needle Position Figure 6: When Does a Needle Fall on a Line? Chapter 6 Iteration 65
Needle Position q Needle length = 1, distance between lines = 2 q Generate random ylow between 0 and 2 q Generate random angle between 0 and 180 degrees q yhigh = ylow + sin( ) q Hit if yhigh ≥ 2 Chapter 6 Iteration 66
File Needle. java 01: import java. util. Random; 02: 03: /** 04: This class simulates a needle in the Buffon needle experiment. 05: */ 06: public class Needle 07: { 08: /** 09: Constructs a needle. 10: */ 11: public Needle() 12: { 13: hits = 0; 14: tries = 0; 15: generator = new Random(); 16: } 17: Continued… Chapter 6 Iteration 67
File Needle. java 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: /** Drops the needle on the grid of lines and remembers whether the needle hit a line. */ public void drop() { double ylow = 2 * generator. next. Double(); double angle = 180 * generator. next. Double(); // Computes high point of needle double yhigh = ylow + Math. sin(Math. to. Radians(angle)); if (yhigh >= 2) hits++; tries++; } /** Gets the number of times the needle hit a line. @return the hit count Continued… */ Chapter 6 Iteration 68
File Needle. java 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: } public int get. Hits() { return hits; } /** Gets the total number of times the needle was dropped. @return the try count */ public int get. Tries() { return tries; } private Random generator; private int hits; private int tries; Chapter 6 Iteration 69
File Needle. Tester. java 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: /** This program simulates the Buffon needle experiment and prints the resulting approximations of pi. */ public class Needle. Tester { public static void main(String[] args) { Needle n = new Needle(); final int TRIES 1 = 10000; final int TRIES 2 = 1000000; Chapter 6 Iteration Continued… 70
File Needle. Tester. java 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: } for (int i = 1; i <= TRIES 1; i++) n. drop(); System. out. printf("Tries = %d, Tries / Hits = %8. 5 fn", TRIES 1, (double) n. get. Tries() / n. get. Hits()); for (int i = TRIES 1 + 1; i <= TRIES 2; i++) n. drop(); System. out. printf("Tries = %d, Tries / Hits = %8. 5 fn", TRIES 2, (double) n. get. Tries() / n. get. Hits()); } Output Tries = 10000, Tries / Hits = 3. 08928 Tries = 1000000, Tries / Hits = 3. 14204 Chapter 6 Iteration 71
Self Check 9. How do you use a random number generator to simulate the toss of a coin? 10. Why is the Needle. Tester program not an efficient method for computing π? Chapter 6 Iteration 72
Answers 9. 10. int n = generator. next. Int(2); // 0 = heads, 1 = tails The program repeatedly calls Math. to. Radians(angle). You could simply call Math. to. Radians(180) to compute π Chapter 6 Iteration 73
Correctness Proof q Sometimes possible to prove that a loop (or entire program) is correct q If possible, this is way better than testing q But not practical in most realistic cases q Only feasible for relatively simple code q Seldom used in practice § But is used in some high-security situations Chapter 6 Iteration 74
- Modified secant method
- Iteration demo
- Solving recurrence relations by iteration
- Recursion and loop difference
- Sequence selection iteration
- Value iteration
- Value iteration
- Orthogonal iteration
- Value iteration algorithm
- Value iteration algorithm
- What is the purpose of an iteration recap?
- Iteration math
- What is iteration in computer science
- Recursion
- Iteration definition computer science
- Avle chapter 6
- Fixed iteration method
- Lambda iteration
- Final iteration definition
- Replace recursion with iteration
- Value iteration
- Value iteration
- Lanczos iteration
- Embarrassingly parallel examples
- Iteration abstraction
- Iterative statements in python
- Get sequence get another sequence pseudocode
- Chaos theory jurassic park
- Iteration bbc
- Iteration demo
- Iteration
- Iteration statements in java
- Release planning is done in iteration 0
- Arnoldi iteration example
- Lambda iteration
- Lambda iteration
- Iteration control structures
- Iteration control structures
- Entity life history
- Modern process transitions in spm
- Iteratio.n5
- Value iteration
- Iteration
- Iteration
- Iteration
- 5 levels of agile planning
- Gauss seidel pseudocode
- Checker team
- Value iteration
- Structurell
- Sequence selection iteration
- Fixed point iteration method
- Tail recursion vs iteration
- Power iteration clustering
- Jacobi method
- Math 175
- Iteratio.n5
- Chapter 5 python for everybody
- Policy iteration
- Scratch iteration
- Fitted q iteration
- Iteration variable
- The red tent summary chapter by chapter
- Great gatsby chapter 8-9 summary
- Chapter 10 chapter assessment chemical reactions answers
- Chapter 11 stoichiometry test
- Chapter 9 chemical reactions study guide
- Chapter 7 similarity
- Chapter 6 career readiness chapter review answers
- Chapter 7 ionic and metallic bonding chapter answer key
- Chapter 9 surface water chapter assessment answer key
- Chapter 2 representing motion chapter assessment
- Chemistry: the central science chapter 14 answers
- Chapter 7 ionic and metallic bonding chapter answer key
- Chapter 4 population ecology worksheet answer key