+ All Categories
Home > Documents > Repetition

Repetition

Date post: 23-Feb-2016
Category:
Upload: paiva
View: 27 times
Download: 0 times
Share this document with a friend
Description:
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/ - PowerPoint PPT Presentation
Popular Tags:
30
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
Transcript
Page 1: Repetition

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

Page 2: Repetition

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

Page 3: Repetition

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

Page 4: Repetition

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

Page 5: Repetition

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

Page 6: Repetition

Logic of a while Loop

statement

true false

conditionevaluated

CSC 1051 M.A. Papalaskari, Villanova University

Page 7: Repetition

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

Page 8: Repetition

The while Statement

int count = 1;while (count <= 3){ System.out.println (count); count++;}

CSC 1051 M.A. Papalaskari, Villanova University

Page 9: Repetition

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

Page 10: Repetition

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

Page 11: Repetition

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

Page 12: Repetition

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

Page 13: Repetition

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

Page 14: Repetition

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

Page 15: Repetition

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

Page 16: Repetition

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

Page 17: Repetition

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

Page 18: Repetition

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

Page 19: Repetition

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

Page 20: Repetition

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

Page 21: Repetition

What’s wrong with this code?

int count = 1;while (count <= 10) System.out.println (count); count++;

CSC 1051 M.A. Papalaskari, Villanova University

Page 22: Repetition

What’s wrong with this code?

int count = 1;while (count <= 10);{ System.out.println (count); count++;}

CSC 1051 M.A. Papalaskari, Villanova University

Page 23: Repetition

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

Page 24: Repetition

CSC 1051 M.A. Papalaskari, Villanova University

Page 25: Repetition

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

Page 26: Repetition

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

Page 27: Repetition

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?

Page 28: Repetition

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

Page 29: Repetition

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

Page 30: Repetition

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


Recommended