CSC 1051 M.A. Papalaskari, Villanova University
Repetition
CSC 1051 – Data Structures and Algorithms IDr. Mary-Angela PapalaskariDepartment of Computing SciencesVillanova 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
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 1
boolean 2
statement 1
statement 2true
true
false
false
statement 3
This slide dapted from Doug Clark’s course http://www.cs.princeton.edu/courses/archive/spring13/cos126/lectures.php
Review
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 University
year interest balance0 $10,000.00
1 $500.00 $10,500.00
2 $525.00 $11,025.00
3 $551.25 $11576.25
4 $578.81 $12,155.06
This example is adapted from Cay Horstmann’s Big Java, Early Objects, 5th edition
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
Logic of a while Loop
statement
true false
conditionevaluated
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
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Initialize count
count 1
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
count <= 3 is true
count 1
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Print count
Output:1
count 1
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Increment count
Output:1
count 2
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
count <= 3 is true
count 2
Output:1
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Print count
Output:12
count 2
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Increment count
Output:12
count 3
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
count <= 3 is true
count 3
Output:12
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Print count
Output:123
count 3
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
Increment count
Output:123
count 4
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement
int count = 1;while (count <= 3){ System.out.println (count); count++;}
count <= 3 is false
count 4
Output:123
CSC 1051 M.A. Papalaskari, Villanova University
The while Statement “unraveled”int count = 1;while (count <= 3){ System.out.println(count); count++;}
Output:123
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
count 4
count 3
count 2
count 1
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
N 2N 3N
0 1 11 2 32 4 93 8 274 16 81
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
Correction: May 2, 2013An 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, 2013http://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?
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
Nested loopsExample: Investment problem repetitionthe repeated action (calculating the number of
years it take for investment to double) involves repetition
General pattern for algorithms: A nested loopwhile (condition for repeating action)
initialize variables (?)while (condition for reaching goal)
calculationsprint 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).
– 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