Repetition Chapter 6 12/06/16 & 12/07/16 1 1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 1 1
Chapter Contents The Logic of a Loop The while Statement A Problem Solved: A Guessing Game Errors in Loops Off-by-One Errors Infinite Loops Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 2 2
The Logic of a Loop Iteration repeat a group of statements Use a controlling statement Figure 12-1, Steps of a typical loop Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 3 3
The while Statement Controls the execution of a while loop Syntax Logic of a while statement Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 4 4
The while Statement Consider – exactly what numbers are printed by this example? Note often used style of incrementing counter = counter + 1; // and counter++; // have same result Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 5 5
The while Statement Counting loops Sentinel controlled loops Loop must run specific number of times Variable incremented/decremented required number of times Sentinel controlled loops Task must be repeated until a certain condition is true That condition is the sentinel Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 6 6
A Guessing Game One person (computer) thinks of a number Other person tries to guess the number First person responds “too high” “too low” “that’s it” Process repeats until guess is correct We seek a Java program to play the game Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 7 7
A Guessing Game Pseudocode solution Let's write the code. 8 8 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 8 8
Off by One Errors Common mistake in loop design Incorrect condition is tested Possible misuse of ≤ vs < Incorrect initial value Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 9 9
Off by One Errors Placement of the increment statement //This main() is suppose to count by 10 from 10 to 100 public static void main(String[] args) { int n = 10; while(n <= 100){ n = n + 10; System.out.println(n); } See: MisplacedInc.java Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 10 10
public class ErrorMisplaceInc { //This program is suppose to count by 10 from 10 to 100 //It still has problems. public static void main(String[] args) { int n = 10; while(n != 99) { System.out.println(n); n = n + 10; } 11
Infinite Loops More mistakes in loop design Again, incorrect condition tested Check for equality that can never happen Checking for equality with reals Should check for “closeness” Missing increment/decrement statement Extra semicolon Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 12 12
Question: What is the ouput? int num =0, max = 20; while (num < max){ System.out.println(num); num = num + 4; } 13 13
Participation Imagine a program that read the population of a city using the following statements: System.out.println(“Enter the population:”); Int population = scan.nextInt(); Write a while loop after these statements that ensures the population is positive. If the user enters a population that is either negative or zero, ask the user to enter a non-negative value. Keep asking until a non-negative is entered. Echo output the number that is finally stored. 14 14
Strings charAt(n) – returns char at position n. Line.charAt(4) →'g'
Participation Work with a partner. Write a program that inputs a word. Use a while loop and the charAt() method to output the word spelled backwards. Sample execution: Input:desserts Output:stressed 16 16
Estimation of Square Root (Skip) Can not discuss finding roots as the text does because it requires calculus. Can use an algorithm for finding the square root of a number that is based on the same principle. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 17 17
Estimation of Square Start with an estimate. Get another estimate based on that estimate. Repeat this until the two estimates are within the require error tolerance. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 18 18
Estimation of Square Input s, tolerance x = s //First estimate for x is the input newX = ½(x+ s/x) // Second estimate While( |newX – x| < tolerance) X = newX //Keep new est. newX = ½(x+ s/x) //Find next est. 5)Output x “is the sqrt of “ s Try it with 7. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 19 19
Nested Loops(Skip) Consider code to generate a two dimensional pattern of asterisks Nested loop solution Inner loop displays each star in a row Outer loop drives the creation of the rows View code to create pattern of stars, Listing 12-2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 20 20
Scope of a Variable(skip) Scope portion of program where variable can be used Figure 12-13 Scope of Variables Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 21 21
Repetition Chapter 6 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 22 22