Download presentation
Presentation is loading. Please wait.
Published byKenneth Walton Modified over 9 years ago
1
CSC 1051 M.A. Papalaskari, Villanova University 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
2
Control flow Sequence of statements that are actually executed in a program Conditional and Repetition statements: enable us to alter control flow CSC 1051 M.A. Papalaskari, Villanova University statement 1 statement 2 statement 3 statement 4 boolean 1boolean 2 statement 1 statement 2 true false statement 3 This slide dapted from Doug Clark’s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php Review
3
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? CSC 1051 M.A. Papalaskari, Villanova UniversityThis example is adapted from Cay Horstmann’s Big Java, Early Objects, 5 th edition
4
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
5
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
6
Logic of a while Loop statement true false condition evaluated CSC 1051 M.A. Papalaskari, Villanova University
7
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
8
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University
9
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Initialize count count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University
10
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University
11
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 Output: 1 count 1 count 1 CSC 1051 M.A. Papalaskari, Villanova University
12
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 Output: 1 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University
13
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 2 count 2 Output: 1 Output: 1 CSC 1051 M.A. Papalaskari, Villanova University
14
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 Output: 1 2 count 2 count 2 CSC 1051 M.A. Papalaskari, Villanova University
15
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 Output: 1 2 count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University
16
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is true count 3 count 3 Output: 1 2 Output: 1 2 CSC 1051 M.A. Papalaskari, Villanova University
17
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Print count Output: 1 2 3 Output: 1 2 3 count 3 count 3 CSC 1051 M.A. Papalaskari, Villanova University
18
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } Increment count Output: 1 2 3 Output: 1 2 3 count 4 count 4 CSC 1051 M.A. Papalaskari, Villanova University
19
The while Statement int count = 1; while (count <= 3) { System.out.println (count); count++; } count <= 3 is false count 4 count 4 Output: 1 2 3 Output: 1 2 3 CSC 1051 M.A. Papalaskari, Villanova University
20
The while Statement “unraveled” int count = 1; while (count <= 3) { System.out.println(count); count++; } int count = 1; while (count <= 3) { System.out.println(count); count++; } Output: 1 2 3 Output: 1 2 3 CSC 1051 M.A. Papalaskari, Villanova University 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 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 4 count 4 count 3 count 3 count 2 count 2 count 1 count 1
21
What’s wrong with this code? int count = 1; while (count <= 10) System.out.println (count); count++; CSC 1051 M.A. Papalaskari, Villanova University
22
What’s wrong with this code? int count = 1; while (count <= 10); { System.out.println (count); count++; } CSC 1051 M.A. Papalaskari, Villanova University
23
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
25
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
26
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 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
27
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 CSC 1051 M.A. Papalaskari, Villanova University Sometimes people need more than a second chance (a third? a fourth?...) 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?
28
What if we want to do a calculation over and over again? Example: Calculating GPA for many students (how many? when do you stop?) Possible approaches: 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
29
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
30
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). –Always do 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.