1 / 29

Repetition

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/

flann
Download Presentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. Topic Thread • 2.1 Character Strings • 2.2 Variables, Assignment • 2.3 Data Types, in particular int, double • 2.4 Expressions (simple) • 2.5 Data Conversion • 2.6 Interactive Programs • 5.1 Boolean Expressions • 5.2 The if Statement • 5.4 The while Statement CSC 1051 M.A. Papalaskari, Villanova University

  3. Flow of Control The order of statement execution • Unless specified otherwise, the order of statement execution through a method is linear • Some programming statements allow us to: • decide whether or not to execute a particular statement • execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false CSC 1051 M.A. Papalaskari, Villanova University

  4. Flow of Control The order of statement execution • Unless specified otherwise, the order of statement execution through a method is linear • Some programming statements allow us to: • decide whether or not to execute a particular statement • execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false CSC 1051 M.A. Papalaskari, Villanova University

  5. 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

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

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

  8. condition evaluated true false statement Logic of a while Loop CSC 1051 M.A. Papalaskari, Villanova University

  9. 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

  10. The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. Example: Input validation System.out.println(“type in a number > 5”); intnum = scan.nextInt(); while (num <= 5) { System.out.println (“Please try again”); System.out.println (“type a number > 5”); num = scan.nextInt(); } CSC 1051 M.A. Papalaskari, Villanova University

  25. Let’s try this with the Wages.java program //******************************************************************** // Wages.java Author: Lewis/Loftus // // Demonstrates the use of an if-else statement. //******************************************************************** import java.text.NumberFormat; import java.util.Scanner; public class Wages { //----------------------------------------------------------------- // Reads the number of hours worked and calculates wages. //----------------------------------------------------------------- public static void main (String[] args) { final double RATE = 8.25; // regular pay rate final int STANDARD = 40; // standard hours in a work week Scanner scan = new Scanner (System.in); double pay = 0.0; continue CSC 1051 M.A. Papalaskari, Villanova University

  26. continue System.out.print ("Enter the number of hours worked: "); int hours = scan.nextInt(); System.out.println (); // Pay overtime at "time and a half" if (hours > STANDARD) pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5); else pay = hours * RATE; NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println ("Gross earnings: " + fmt.format(pay)); } } What if input is negative or zero? CSC 1051 M.A. Papalaskari, Villanova University

  27. What if we want to do a calculation over and over again? • Example: Keep calculating wages until user quits program (infinite loop). • Example: Keep calculating wages and ask each time whether to keep going. • Example: Keep calculating wages until user inputs zero for the hours • Example: Calcultate wages for 20 employees CSC 1051 M.A. Papalaskari, Villanova University

  28. 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

  29. 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

More Related