300 likes | 486 Views
Repetition. CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/
E N D
Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/ Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus CSC 1051 M.A. Papalaskari, Villanova University
Review Control flow • Sequence of statements that are actually executed in a program • Conditional and Repetition statements: enable us to alter control flow true statement 1 false statement 1 statement 2 boolean 2 boolean 1 true statement 3 statement 2 false statement 4 statement 3 This slide dapted from Doug Clark’s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php CSC 1051 M.A. Papalaskari, Villanova University
Example • Investment problem: You put $10,000 into a bank account that earns 5% interest per year. • … How many years does it take for the account balance to be double the original? This example is adapted from Cay Horstmann’sBig Java, Early Objects, 5th edition CSC 1051 M.A. Papalaskari, Villanova University
Example • Investment problem: You put $10,000 into a bank account that earns 5% interest per year. How many years does it take for the account balance to be double the original? • Algorithm: CSC 1051 M.A. Papalaskari, Villanova University
The while Statement • A while statement has the following syntax: while ( condition ) statement; • If the condition is true, the statement is executed • Then the condition is evaluated again, and if it is still true, the statement is executed again • The statement is executed repeatedly until the condition becomes false CSC 1051 M.A. Papalaskari, Villanova University
condition evaluated true false statement Logic of a while Loop CSC 1051 M.A. Papalaskari, Villanova University
Example • A counting loop that prints the numbers 1, 2, 3,… Algorithm: • initialize a counter to 1 • while the counter <= upper limit • print counter • increment counter CSC 1051 M.A. Papalaskari, Villanova University
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 1 Initialize count int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 1 int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 1 int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 2 int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 2 int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true Output: 1 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 2 int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 3 int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 3 int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 3 int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 3 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 4 int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 3 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement count 4 int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is false Output: 1 2 3 CSC 1051 M.A. Papalaskari, Villanova University
The while Statement “unraveled” int count = 1; TEST:(count <= 3) true { System.out.println(count); count++; } TEST:(count <= 3) true { System.out.println(count); count++; } TEST:(count <= 3) true { System.out.println(count); count++; } TEST:(count <= 3) false EXIT LOOP count 1 int count = 1; while (count <= 3) { System.out.println(count); count++; } count 2 count 3 Output: 1 2 3 count 4 CSC 1051 M.A. Papalaskari, Villanova University
What’s wrong with this code? int count = 1; while (count <= 10) System.out.println (count); count++; CSC 1051 M.A. Papalaskari, Villanova University
What’s wrong with this code? int count = 1; while (count <= 10); { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University
Example • Table of powers: Compute the powers of 2 and the powers of 3 and print a table like this: CSC 1051 M.A. Papalaskari, Villanova University
If the condition of a while loop is false initially, the statement is never executed int count = 8; while (count <= 3) { System.out.println (count); count++; } • Therefore, the body of a while loop will execute zero or more times CSC 1051 M.A. Papalaskari, Villanova University
Example: Input validation – First try System.out.println(“Enter lifestyle code”); System.out.println (“0=bad; 1=ok; 2=super fit”); int lifestyle = scan.nextInt(); if (lifestyle <0 || lifestyle > 2) { System.out.println (“Please try again”); System.out.println (“0=bad; 1=ok; 2=super fit”); num= scan.nextInt(); } CSC 1051 M.A. Papalaskari, Villanova University
Sometimes people need more than a second chance (a third? a fourth?...) Correction: May 2, 2013 An earlier version of this article referred incorrectly to the products sold at By Brooklyn. The store does not sell dandelion and burdock soda, lovage soda syrup, and Early Bird granola “gathered in Brooklyn.” An earlier version also referred incorrectly to the thoroughfare that contains the thrift shop Vice Versa. It is Bedford Avenue, not Bedford Street, or Bedfoprd Avenue, as stated in an earlier correction. New York Times "How I Became a Hipster" Published: May 1, 2013 http://www.nytimes.com/2013/05/02/fashion/williamsburg.html?_r=0 • how about giving the user more chances: allow them to repeatedly enter the input while they are getting it wrong • what is a small change we can make on the previous example to effect that? CSC 1051 M.A. Papalaskari, Villanova University
What if we want to do a calculation over and over again? Example: Calculating GPA for many students (how many? when do you stop?) Possibleapproaches: • Keep accepting new inputs (for each student) and calculating and printing corresponding GPA until user quits program (infinite loop). • Same, but ask each time whether to keep going. • Same, but quit if the user inputs -1 for the credits (signals end) • Calculate GPA for exactly 20 students CSC 1051 M.A. Papalaskari, Villanova University
Nested loops Example: Investment problem repetition • the repeated action (calculating the number of years it take for investment to double) involves repetition General pattern for algorithms: A nested loop while (condition for repeating action) initialize variables (?) while (condition for reaching goal) calculations print results CSC 1051 M.A. Papalaskari, Villanova University
Homework • Read Section 5.4, • the example of Nested loops (PalindromeTester.java, pp 237-238) uses some concepts we have not covered, so you can skip that for now). • Alwaysdo all self-review exercises when you review material • Do end of chapter Exercises EX 5.7 – 5.11 CSC 1051 M.A. Papalaskari, Villanova University