loops “life is just one damn thing after another.” -- mark twain “life isn’t just one damn...

28
LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over and over again.” -- Edna St. Vincent Millay

Upload: agatha-strickland

Post on 03-Jan-2016

230 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

LOOPS

“Life is just one damn thing after another.”

-- Mark Twain

“Life isn’t just one damn thing after another...it’s the same damn thing over and over and over again.”

-- Edna St. Vincent Millay

Page 2: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

LOOPS

• while loops

• do while loops

• for loops

Page 3: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 3 of 29

Turtle (courtesy of Logo)

• Before we see loops, we need some tools– Turtle: will draw on the screen for us; based

on Seymour Papert’s Logo, a language for beginners*

– GeometricPatterns: instructions for turtle

• Turtles know where they are and what direction they are facing

• Turtles draw lines behind them as they move around screen

• Now let’s look at Turtle’s public interface

*See his Mindstorms, a provocative book on how kids “learn by doing”, and also the instrumented Legos, Mindstorms.

Page 4: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 4 of 29

Turtle’s Public Interface (1 of 2)

• We have written Turtle class:

public class Turtle extends gfx.Shape { /* some instance variables here */

/* constructor for Turtle takes panel where turtle “lives” as parameter to setup an

association */

public Turtle(DrawingPanel panel) { // some code here, including storing panel }

/* reset turtle to center of container */ public void home() { // some code here }

/* turn right specified number of degrees */ public void right(int degrees) { // some code here }

/* turn left specified number of degrees */ public void left(int degrees) { // some code here }

// continued...

Page 5: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 5 of 29

Turtle’s Public Interface (2 of 2)

/* move forward specified distance, drawing line as turtle moves */ public void forward(int distance) { // some code here }

/* move backward specified distance, drawing line as turtle moves */ public void back(int distance) { // some code here }

/* move turtle to specified position without drawing line */ public void setLocation(java.awt.Point loc) { // some code here }

/* returns turtle’s drawing panel */ public DrawingPanel getPanel() { // some code here }

} // end of class Turtle

Page 6: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 6 of 29

Drawing with Turtle

• Need class to tell Turtle how to draw some basic shapes

• First, determine what shapes we want– this lecture: square, random walk– next lecture: recursive spiral, tree, fractal

• How will we code it?– create GeometricPatterns class which defines

methods for drawing each shape– methods control turtle, so it needs a reference to

the instance, _turtle– geometric patterns do not contain a Turtle, so

use a standard pattern of passing a Turtle reference in the constructor and storing it.

• Time for some code!

public class GeometricPatterns { private Turtle _turtle; // draws each pattern public GeometricPatterns(Turtle newTurtle){ _turtle = newTurtle; } // methods for each geometric pattern to // follow...}

Page 7: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 7 of 29

A Repetitive Solution

• Let’s write the drawSquare method in the GeometricPatterns class

• Brute force: write line of code for each side

public void drawSquare(int sideLen) { _turtle.forward(sideLen); _turtle.right(90); _turtle.forward(sideLen); _turtle.right(90); _turtle.forward(sideLen); _turtle.right(90); _turtle.forward(sideLen); _turtle.right(90);}

• What if we wanted to make more general method that handles pentagons or octagons?– need to call forward and right for each side– cannot fix how many sides we need in generic

method

• There must be an easier way!

Page 8: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 8 of 29

Looping

• Causes computer to execute section of code repeatedly– uses booleans (true and false) as loop

conditions; when boolean is false, the loop condition equals the exit condition and the loop is terminated

– as with conditionals, section of code can be single line or many lines enclosed in curly braces

– section of code executed is typically called the loop’s body

• Three loop structures in Java– while loop– do while loop– for loop

• Differ only in relation between body and loop condition– some loops execute body and then test; others

test before executing body

• Let’s look at while loops first

Page 9: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 9 of 29

The while loop

• Execute while certain condition is true– tests loop condition before executing body– if loop condition is false first time through, the

body is not executed at all

while (<loop condition>) {

<loop body>

}

• Examples of loop conditions:– numClasses < 30– peopleStanding <= maxPeople– this.checkAmount() <= acctBalance– isSquare() // predicate; returns boolean

• Multiple conditions can be combined using logical operators (and, or, not)

(numClasses >= 3) && (numClasses <=5)

(peopleStanding <= maxPeople) || (maxPeople < 50)

Page 10: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 10 of 29

A while Flowchart

• while loops continue while loop condition is true

• Loop condition can be any boolean expression

Is <loopcondition>

TRUE?

<loop body>

<rest of program>

No

Yes

<previous statement>

Page 11: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 11 of 29

Syntax: Random Walk Using While

• Method of GeometricPatterns class:– draws random lines while turtle is within its frame

public void randomWalk() { DrawingPanel turtlePanel =

_turtle.getPanel(); /* while _turtle’s position is inside its frame, move _turtle randomly. */ while (turtlePanel.contains(

_turtle.getLocation())) { _turtle.forward

((int)(Math.random()*15)); //cast

_turtle.right((int)(Math.random()*360));

//cast }}

• On last step of walk, turtle will end up moving forward out of panel but line will not be drawn– it is clipped by Swing since we don’t explicitly tell it

to wrap– no point in continuing to walk outside of the panel

Page 12: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 12 of 29

The do while Loop

• do while always executes loop body at least once by switching order of test and body

• <loop condition> is boolean expression.

Is <loopcondition>

TRUE?Yes

<loop body>

No

<rest of program>

<previous statement>

Page 13: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 13 of 29

Example: Another Random Walk

• Method of GeometricPatterns class:– draws random lines while turtle is within its

panel– starts turtle in center of drawing panel, so

first step is guaranteed to be within drawing panel

public void centeredRandomWalk() {DrawingPanel panel =

_turtle.getPanel();

// moves turtle to Frame’s center _turtle.home();

// moves turtle randomly within frame do { _turtle.forward

((int)(Math.random()*15)); _turtle.right

((int)(Math.random()*360));

} while (panel.contains(_turtle.getLocation()));

//note semicolon at end of while statement

}

Page 14: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 14 of 29

• In both loops– stops executing body if loop condition is false– you must make sure loop condition becomes false

by some computations lest you “infinite loop”– infinite loop means your loop’s loop condition is

such that it will never turn false, i.e., the exit condition never occurs

• do while– body always executed at least once– loop condition tested at bottom of loop

• while– may not execute at all– loop condition tested before body; loop condition

variables must be set before loop entry– useful for screening bad data that might cause

statements within loop to fail (e.g., while (ref != null) )

• What is the difference between these two?

while(!_stomachIsFull) do {

this.eatIceCream(); this.eatIceCream();

} while (!_stomachIsFull);

do while vs. while

Page 15: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 15 of 29

for Loops

• Most specialized loop construct

for (<init-expr>; <loop condition>; <update>) {

<loop body>

}

– <init-expr>: expression for setting initial value of loop counter (traditionally use single character identifier; e.g., i); counter aka index

– <loop condition>: (true or false) test involving loop counter to determine if loop should execute

– <update>: expression that modifies loop counter– <init-expr> is executed at start of loop code, <loop condition> is checked at the start of every loop (including the first), and <update> is run at the end of every <loop body>, just before returning to the top of the loop .

• Typically used to execute loop body predetermined number of times– while and do while loops can execute body for

undetermined number of times; based on boolean

Page 16: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 16 of 29

drawSquare Revisited

• Better way of drawing square than explicitly drawing each side:

public void drawSquare(int sideLen) {

/* start with integer i initialized to 0;

execute as long as i < 4; each execution

increments i by 1 */

for (int i = 0; i < 4; i++) {

_turtle.forward(sideLen);

_turtle.right(90);

}

}

Page 17: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 17 of 29

for Flowchart

• for loop has four parts– initialize value of counter– test loop condition– loop body– counter update

<init-counter>

<update counter>

<loop body>

<rest of program>

Yes

No

Is <loopcondition>

TRUE?

Page 18: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 18 of 29

Choosing the Right Loop

• for loop is called definite loop because you can typically predict how many times it will loop. while and do while loops are indefinite loops, as you do not know a priori when they will end.

• for loop is typically used for math-related loops like counting finite sums

• while loop is good for situations where boolean condition could turn false at any time

• do while loop is used in same type of situation as while loop, but when code should execute at least once

• When more than one type of loop will solve problem, use the cleanest, simplest one

Page 19: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 19 of 29

Syntax: Nested Loops

• Loops, much like we saw with if statements, can be nested!

• Example: drawFilledSquare

public void drawFilledSquare(int sideLen) {

// fill in concentric squares for (int i = 0; i < (sideLen / 2); i++) {

// drawSquare (slide #16) contains a loop this.drawSquare(sideLen - (2*i));

// position turtle for next iteration _turtle.right(90); _turtle.forward(1); _turtle.left(90); _turtle.forward(1); }}

• What does this do?

Page 20: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 20 of 29

Nested Loops in English

• Turtle is represented by

• What is outer loop doing?– first draws concentric square

– then the last 4 lines move turtle right and up 1 unit

• drawFilledSquare draws square using nested loop

Turtle starts upright

Rotate 90 degrees right

Move forward 1 unit

Rotate 90 degrees left

Move forward 1 unit

Page 21: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 21 of 29

Decrementing Counter

• We can count backwards in our loop too– just change counter update expression– in fact, we can update however we want

public void countFingers() { /* change counter to decrement, and change the loop condition accordingly */ for (int i = 4; i > 0; i--) hand.hideFinger(i); // hide a finger}

• for loops end in one of two ways– when counter value equals limit (for < or >)– when counter value “goes past” limit (for <= or >=)– thus, countFingers() would hide 5 fingers if

we used i >= 0 (not a good idea because Grover only has 4 fingers)

– Beware of such “off-by-one” errors!!!

Four little fingers,Three little fingers,Two...

Page 22: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 22 of 29

break

• As with switch, break causes immediate exit from a flow-of-control structure (e.g., while, do while, for )

• Example:

//we’d like to eat 10 cookies (they’re small!)for (int i = 0; i < 10; i++) { if(_cookieJar.numberOfCookies() == 0 ){ break; // if there are no cookies left, // we should break out of the loop // because we can’t eat any more! }this.eatACookie(); //we know there are some

//left}// execution continues here after loop is done// or after break statement

• Execution continues with first line of code after the structure

• There are other ways to do this loop…

Page 23: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 23 of 29

continue

• When executed in while, for, do while structure, continue skips remaining statements in body of that structure, and proceeds with next iteration of loop– useful if there is list of data that you are looping over and

you want to skip processing of data that is somehow “illegal”

• Example:// we’d like to try on shirts that hang on a// rackfor (int i = 0; i < 20; i++) { if( !rack.isShirtOnHanger(i) ) { // if there’s no shirt on the current // hanger, skip to the next iteration

continue; }// only do this if there’s a shirt on the

// hangerShirt toTry = rack.shirtOnHanger(i); //get

// the shirtthis.tryOnShirt( toTry ); // try the shirt on

}// more code here

• In while and do while structure, execution continues by evaluating loop-continuation condition

• In for structure, execution continues by incrementing counter and then evaluating loop condition

Page 24: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 24 of 29

Boolean Flags

• A boolean flag is a boolean variable that denotes a condition (e.g., done, working, available)– set in one place, tested in another

• Boolean flags can also be used as the loop condition

• Example (implementing a for loop, using while):

boolean done = false;int i = 0;while (!done) { i++; if (i == 5) done = true;}

• Notice that boolean flag is set within loop– in previous slides all checking was done through

delegation, here we do it ourselves

Page 25: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 25 of 29

Empty Intervals

• What happens if we try to add integers in this loop?

public int sum() { int temp_sum = 0; for (int i = 1; i < 1; i++) temp_sum += i;

return temp_sum;}

• Answer: Body of loop is not executed

• Why?– boolean is false for initial value of counter

• Correct example:/* This method sums all numbers from 1 up to and including the number specified */

public int sum(int number) { int temp_sum = 0; for (int i = 1; i <= number; i++) temp_sum += i;

return temp_sum;}

Page 26: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 26 of 29

Off-by-one Errors

• Occur when loop executes one too many or one too few times

• Example: Add even integers from 2 to number, inclusive...count = 2;result = 0;while (count < number) { result += count; count += 2;}

• Produces incorrect result if number is assigned an even value. Values from 2 to number-2 will be added (i.e., number excluded)

• Should be:while (count <= number) {...}

• Now, value of number is included in summation

Page 27: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 27 of 29

Syntax: Other Loop Errors

• Make sure test variables have proper values before loop is entered...product = 0;do { product *= 2;} while (product < 100);

/* what will happen here? */

• Make sure tests check proper conditions...for (int i = 1; i != 100; i += 2) { // do something here}

/* will we ever get here? */

• ALWAYS HAND SIMULATE first, last, and typical case through a loop to avoid off-by-one or infinite loop errors– the first and last cases of a loop’s execution are

called boundary or edge conditions– hand simulation doesn’t just apply to loops, use it

for everything! Trust us – it saves debugging time!!!

Page 28: LOOPS “Life is just one damn thing after another.” -- Mark Twain “Life isn’t just one damn thing after another...it’s the same damn thing over and over

October 13, 2005Loops 28 of 29

Which Loop to Use?

• You want to bake a pie for each of your twelve friends

• Your job is to stand at the end of the bowling alley, and pick up all the pins, one by one, that have been knocked over

• Ask a question. Keep asking while the answer is incorrect