Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.

Slides:



Advertisements
Similar presentations
Chapter 13 ARRAY LISTS AND ARRAYS. CHAPTER GOALS To become familiar with using array lists to collect objects To learn about common array algorithms To.
Advertisements

Chapter 13 ARRAY LISTS AND ARRAYS CHAPTER GOALS –To become familiar with using array lists to collect objects –To learn about common array algorithms –To.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Chapter 7 – Arrays.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Chapter 6 Iteration Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
ITERATION CSC 171 FALL 2004 LECTURE 10. Simple Branching If (test){ A;} start end A; test.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CHAPTER 17 RECURSION CHAPTER GOALS –To learn about the method of recursion –To understand the relationship between recursion and iteration –To analysis.
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
CHAPTER 17 RECURSION. CHAPTER GOALS To learn about the method of recursion To understand the relationship between recursion and iteration To analysis.
Chapter 10 Testing and Debugging. Chapter Goals ► To learn how to carry out unit tests ► To understand the principles of test case selection and evaluation.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Repetition Statements while and do while loops
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
If Statement if (amount
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 7 - Iteration.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Chapter 8 – Arrays and Array Lists
Chapter Six: Iteration
Chapter Goals To implement while, for, and do loops
Lecture Notes – Basics (Ch1-6)
Chapter 6 – Loops.
Repetition-Counter control Loop
Selected Topics From Chapter 6 Iteration
LOOPS.
Chapter Six - Iteration
Chapter 7 Iteration.
Repetition Statements
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
6.2 for Loops Example: for ( int i = 1; i
Loops and Iteration CS 21a: Introduction to Computing I
Presentation transcript:

Chapter 6 Iteration

Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand nested loops To learn how to process input To implement simulations

Investment with Compound Interest Invest $10,000, 5% interest, compounded annually When will the balance be at least $20,000?

while Statement while (condition) statement; repeats the statement while the condition is true while (balance < targetBalance) { year++; double interest = balance * rate / 100; balance = balance + interest; }

File Investment.java 1 /** 2 A class to monitor the growth of an investment that 3 accumulates interest at a fixed annual rate 4 */ 5 public class Investment 6 { 7 /** 8 Constructs an Investment object from a starting balance and

9 interest rate. aBalance the starting balanece aRate the interest rate in percent 12 */ 13 public Investment(double aBalance, double aRate) 14 { 15 balance = aBalance; 16 rate = aRate; 17 years = 0; 18 } 19

20 /** 21 Keeps accumulating interest until a target balance has 22 been reached. targetBalance the desired balance 24 */ 25 public void waitForBalance(double targetBalance) 26 { 27 while (balance < targetBalance)

28 { 29 years++; 30 double interest = balance * rate / 100; 31 balance = balance + interest; 32 } 33 } /** 36 Gets the current investment balance. the current balance 38 */

39 public double getBalance() 40 { 41 return balance; 42 } /** 45 Gets the number of years this investment has accumulated 46 interest.

the number of years since the start of the investment 48 */ 49 public int getYears() 50 { 51 return years; 52 } private double balance; 55 private double rate; 56 private int years; 57 }

File InvestmentTest.java 1/** 2 This program computes how long it takes for an investment 3 to double. 4 */ 5 public class InvestmentTest 6 { 7 public static void main(String[] args) 8 {

9 final double INITIAL_BALANCE = 10000; 10 final double RATE = 5; 11 Investment invest = new Investment(INITIAL_BALANCE, RATE); 12 invest.waitForBalance(2 * INITIAL_BALANCE); 13 int years = invest.getYears(); 14 System.out.println("The investment doubled after " 15 + years + " years"); 16 } 17 }

Flowchart for while Loop

Syntax 6.1. The while Statement while (condition) statement Example: while (balance < targetBalance) { year++; double interest = balance * rate / 100; balance = balance + interest; } –Purpose: To execute a statement while a condition is true

Common Error: Infinite Loop while (year < 20) { balance = balance + balance * rate / 100; } while (year > 0) { year++; // oops, meant -- } Loops run forever--must kill program

Common Error: Off-by-1 Error  year = 0; while (balance < targetBalance) { year++; double interest = balance * rate / 100; balance = balance + interest; } System.out.println("Reached target after " + year + " years.");  Should year start at 0 or 1?  Should the test be < or <=?

Avoiding Off-by-1 Error  Run through a simple example: target balance = $20,000, interest rate 50% after one year: balance = $15,000 after two years: balance = $22,500 Therefore: year must start at 0  interest rate 100% after one year: balance = $20,000 loop should stop Therefore: must use <  Think, don't compile and try at random

do Statement Executes loop body at least once: do statement while (condition); Example: Validate input double value; do { String input = JOptionPane.showInputDialog( "Please enter a positive number"); value = Integer.parseInt(input); } while (input <= 0);

Flowchart for do Loop

for Statement  for (initialization; condition; update) statement  Example: for (int i = 1; i <= n; i++) { double interest = balance * rate / 100; balance = balance + interest; }  Equivalent to initialization; while (condition) { statement; update; }

Flowchart for for Loop

File Investment.java 1 /** 2 A class to monitor the growth of an investment that 3 accumulates interest at a fixed annual rate 4 */ 5 public class Investment 6 {

7 /** 8 Constructs an Investment object from a starting balance and 9 interest rate. aBalance the starting balanece aRate the interest rate in percent 12 */ 13 public Investment(double aBalance, double aRate) 14 { 15 balance = aBalance;

16 rate = aRate; 17 years = 0; 18 } /** 21 Keeps accumulating interest until a target balance has 22 been reached. targetBalance the desired balance 24 */

25 public void waitForBalance(double targetBalance) 26 { 27 while (balance < targetBalance) 28 { 29 years++; 30 double interest = balance * rate / 100; 31 balance = balance + interest; 32 } 33 }

34 35 /** 36 Keeps accumulating interest for a given number of years. n the number of years 38 */ 39 public void waitYears(int n) 40 {

41 for (int i = 1; i <= n; i++) 42 { 43 double interest = balance * rate / 100; 44 balance = balance + interest; 45 } 46 years = years + n; 47 } 48

49 /** 50 Gets the current investment balance. the current balance 52 */ 53 public double getBalance() 54 { 55 return balance; 56 } 57

58 /** 59 Gets the number of years this investment has accumulated 60 interest. the number of years since the start of the investment 62 */ 63 public int getYears() 64 { 65 return years; 66 }

67 68 private double balance; 69 private double rate; 70 private int years; 71 }

File InvestmentTest.java 1 /** 2 This program computes how much an investment grows in 3 a given number of years. 4 */ 5 public class InvestmentTest 6 { 7 public static void main(String[] args) 8 { 9 final double INITIAL_BALANCE = 10000;

10 final double RATE = 5; 11 final int YEARS = 20; 12 Investment invest = new Investment(INITIAL_BALANCE, RATE); 13 invest.waitYears(YEARS); 14 double balance = invest.getBalance(); 15 System.out.println("The balance after " + YEARS + 16 " years is " + balance); 17 } 18 }

Syntax 6.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

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); A missing semicolon for (i = 1; i <= 10; sum = sum + i++) System.out.println(sum);

Nested Loops  Create triangle pattern [] [][] [][][] [][][][]  Loop through rows for (int i = 1; i <= n; i++) { // make triangle row }

 Make triangle row is another loop for (int j = 1; j <= i; j++) r = r + "[]"; r = r + "\n";  Put loops together => Nested loops

File Triangle.java 1 /** 2 This class describes triangle objects that can be displayed 3 as shapes like this: 4 [] 5 [][] 6 [][][] 7 */

8 public class Triangle 9 { 10 /** 11 Constructs a triangle. aWidth the number of [] in the last row of the triangle. 13 */ 14 public Triangle(int aWidth) 15 { 16 width = aWidth; 17 }

18 19 /** 20 Computes a string representing the triangle. a string consisting of [] and newline characters 22 */ 23 public String toString() 24 { 25 String r = "";

26 for (int i = 1; i <= width; i++) 27 { 28 // make triangle row 29 for (int j = 1; j <= i; j++) 30 r = r + "[]"; 31 r = r + "\n"; 32 } 33 return r; 34 } private int width; 37 }

File TriangleTest.java 1 /** 2 This program tests the Triangle class. 3 */ 4 public class TriangleTest 5 { 6 public static void main(String[] args) 7 { 8 Triangle small = new Triangle(3); 9 System.out.println(small.toString());

10 11 Triangle large = new Triangle(15); 12 System.out.println(large.toString()); 13 } 14 }

Reading Input Values General pattern: boolean done = false; while (!done) { String input = read input; if (end of input indicated) done = true; else { process input } } "Loop and a half"

File DataSet.java 1 /** 2 Computes the average of a set of data values. 3 */ 4 public class DataSet 5 { 6 /** 7 Constructs an empty data set. 8 */

9 public DataSet() 10 { 11 sum = 0; 12 count = 0; 13 maximum = 0; 14 } /** 17 Adds a data value to the data set x a data value 19 */

20 public void add(double x) 21 { 22 sum = sum + x; 23 if (count == 0 || maximum < x) maximum = x; 24 count++; 25 } /** 28 Gets the average of the added data. the average or 0 if no data has been added 30 */

31 public double getAverage() 32 { 33 if (count == 0) return 0; 34 else return sum / count; 35 } /** 38 Gets the largest of the added data. the maximum or 0 if no data has been added 40 */

41 public double getMaximum() 42 { 43 return maximum; 44 } private double sum; 47 private double maximum; 48 private int count; 49 }

File InputTest.java 1 import javax.swing.JOptionPane; 2 3 /** 4 This program computes the average and maximum of a set 5 of input values. 6 */ 7 public class InputTest 8 {

9 public static void main(String[] args) 10 { 11 DataSet data = new DataSet(); boolean done = false; 14 while (!done) 15 { 16 String input = JOptionPane.showInputDialog("Enter value, Cancel to quit"); 17 if (input == null) 18 done = true;

19 else 20 { 21 double x = Double.parseDouble(input); 22 data.add(x); 23 } 24 } System.out.println("Average = " + data.getAverage()); 27 System.out.println("Maximum = " + data.getMaximum()); 28 } 29 }

String Tokenization  StringTokenizer breaks up string into tokens  By default, white space separates tokens  " " breaks into three tokens: "4.3", "7", "-2" –StringTokenizer tokenizer = new StringTokenizer(); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); process token }

File InputTest.java 1 import java.util.StringTokenizer; 2 import javax.swing.JOptionPane; 3 4 /** 5 This program computes the average and maximum of a set 6 of input values that are entered on a single line. 7 */ 8 public class InputTest 9 {

10 public static void main(String[] args) 11 { 12 DataSet data = new DataSet(); String input = JOptionPane.showInputDialog("Enter values:"); 15 StringTokenizer tokenizer = new StringTokenizer(input); 16 while (tokenizer.hasMoreTokens()) 17 { 18 String token = tokenizer.nextToken();

19 20 double x = Double.parseDouble(token); 21 data.add(x); 22 } System.out.println("Average = " + data.getAverage()); 25 System.out.println("Maximum = " + data.getMaximum()); 26 } 27 }

Traversing Characters in String  s.charAt(i) is the ith character of the string s –for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); process ch }

 Example: Count vowels –int vowelCount = 0; String vowels = "aeiouy"; for (int i = 0; i = 0) vowelCount++; }  s.indexOf(ch) is the index of the first occurrence of ch in s, or -1 if ch doesn't occur in s

Random Numbers  Random number generator Random generator = new Random(); int n = generator.nextInt(a); // 0 <= n < a double x = generator.nextDouble(); // 0 <= x < 1  Throw die (random number between 1 and 6) int d = 1 + generator.nextInt(6);

File Die.java 1 import java.util.Random; 2 3 /** 4 This class models a die that, when cast, lands on a random 5 face. 6 */ 7 public class Die 8 {

9 /** 10 Constructs a die with a given number of sides s the number of sides, e.g. 6 for a normal die 12 */ 13 public Die(int s) 14 { 15 sides = s; 16 generator = new Random(); 17 }

18 19 /** 20 Simulates a throw of the die the face of the die 22 */ 23 public int cast() 24 { 25 return 1 + generator.nextInt(sides); 26 }

private Random generator; 30 private int sides; 31 }

File DieTest.java 1 /** 2 This program simulates casting a die ten times. 3 */ 4 public class DieTest 5 { 6 public static void main(String[] args) 7 { 8 Die d = new Die(6); 9 final int TRIES = 10;

10 for (int i = 1; i <= TRIES; i++) 11 { 12 int n = d.cast(); 13 System.out.print(n + " "); 14 } 15 System.out.println(); 16 } 17 }

Buffon Needle Experiment

Needle Position  Needle length = 1, distance between lines = 2  Generate random y low between 0 and 2  Generate random angle a between 0 and 180 degrees  y high = y low + sin( a)  Hit if y high >= 2

File Needle.java 1 import java.util.Random; 2 3 /** 4 This class simulates a needle in the Buffon needle experiment. 5 */ 6 public class Needle 7 {

8 /** 9 Constructs a needle. 10 */ 11 public Needle() 12 { 13 hits = 0; 14 generator = new Random(); 15 } 16

17 /** 18 Drops the needle on the grid of lines and 19 remembers whether the needle hit a line. 20 */ 21 public void drop() 22 { 23 double ylow = 2 * generator.nextDouble(); 24 double angle = 180 * generator.nextDouble(); 25

26 // compute high point of needle double yhigh = ylow + Math.sin(Math.toRadians(angle)); 29 if (yhigh >= 2) hits++; 30 tries++; 31 } /** 34 Gets the number of times the needle hit a line. the hit count 36 */

37 public int getHits() 38 { 39 return hits; 40 } /** 43 Gets the total number of times the needle was dropped. the try count 45 */

46 public int getTries() 47 { 48 return tries; 49 } private Random generator; 52 private int hits; 53 private int tries; 54 }

File NeedleTest.java 1 /** 2 This program simulates the Buffon needle experiment 3 and prints the resulting approximations of pi. 4 */ 5 public class NeedleTest 6 { 7 public static void main(String[] args) 8 {

9 Needle n = new Needle(); 10 final int TRIES1 = 10000; 11 final int TRIES2 = ; for (int i = 1; i <= TRIES1; i++) 14 n.drop(); 15 System.out.println("Tries / Hits = " 16 + (double)n.getTries() / n.getHits()); 17

18 for (int i = TRIES1 + 1; i <= TRIES2; i++) 19 n.drop(); 20 System.out.println("Tries / Hits = " 21 + (double)n.getTries() / n.getHits()); 22 } 23 }