karel j. robot chapter 6 instructions that repeat

25
Karel J. Robot Chapter 6 Instructions That Repeat

Upload: alaina-pope

Post on 17-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

Task: Have Karel run a mile long hurdle race Old Way: public void runRace() { jumpHurdle(); jumpHurdle(); } New Way: public void runRace() { for (int i=1; i

TRANSCRIPT

Page 1: Karel J. Robot Chapter 6 Instructions That Repeat

Karel J. Robot

Chapter 6Instructions That Repeat

Page 2: Karel J. Robot Chapter 6 Instructions That Repeat

Task: Have Karel run a mile long hurdle race Old Way public void runRace(){ jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle();}

Page 3: Karel J. Robot Chapter 6 Instructions That Repeat

Task: Have Karel run a mile long hurdle race

Old Way:public void runRace(){ jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle(); jumpHurdle();}

New Way: public void runRace(){ for (int i=1; i<=8; i+

+) { jumpHurdle(); }}

Page 4: Karel J. Robot Chapter 6 Instructions That Repeat

Iteration (Loops) Loops repeat a set of instructions Two types of loops:

Definite loops ( for ) perform instructions a definite number of times.

Indefinite loops ( while ) perform instructions an indefinite number of times (until a certain test fails)

Page 5: Karel J. Robot Chapter 6 Instructions That Repeat

for Loops (Known number of iterations)

General form:

for ( <initialize variable>; <test>; <increment>){

<instruction list>}

Page 6: Karel J. Robot Chapter 6 Instructions That Repeat

for Loops (cont.) <initialize variable> sets a

variable/identifier to a certain value (variable is usually count, i, or j)

<test> is a test that is evaluated each time the body is about to be executed (when false, loop is exited)

<increment> changes the loop control variable (usually i++ or i--)

Page 7: Karel J. Robot Chapter 6 Instructions That Repeat

for Loops (cont.) Example:for (int i=1; i<=8; i++){

jumpHurdle ();}

This causes Karel to jump hurdle 8 times

Page 8: Karel J. Robot Chapter 6 Instructions That Repeat

for Loops (cont.) Example:for (int i=1; i<=5; i++){

jumpHurdle ();}

This causes Karel to jump hurdle 5 times

Page 9: Karel J. Robot Chapter 6 Instructions That Repeat

for Loops (cont.)Flow of for loops:

1. Initialization statement executes.2. If test is true, proceed to step 3; if test is

false, go to step 6.3. Instructions in body of loop are executed.4. Increment statement executes.5. Return to step 2.6. Program proceeds to statements after loop.

Page 10: Karel J. Robot Chapter 6 Instructions That Repeat

Example: turnRightpublic void turnRight () { for (int i = 1; i <= 3; i++) { turnLeft(); } }

Page 11: Karel J. Robot Chapter 6 Instructions That Repeat

Example: Harvest a row of beeperspublic void harvestOneRow() { for ( int i=1; i<=5; i++) { move(); pickBeeper(); } }

Page 12: Karel J. Robot Chapter 6 Instructions That Repeat

while Loops (unknown number of iterations)

General form:while ( <test> ){

<instruction list>}

Loop continues until test is false

Page 13: Karel J. Robot Chapter 6 Instructions That Repeat

while Loops (cont.) Example:while (frontIsClear ()){

move ();}

Causes Karel to move continuously until there is a wall in front of it

Page 14: Karel J. Robot Chapter 6 Instructions That Repeat

while Loops (cont.)Flow of while loops

1. If test is true, proceed to step 2; if test is false, go to step 4.

2. Instructions in body of loop are executed.

3. Go to step 1.4. Statements after loop are

executed.

Page 15: Karel J. Robot Chapter 6 Instructions That Repeat

Building a While Loop

Step 1 – Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing).

Step 2 – Use the opposite form of step 1’s result as the <test> for the loop.

Page 16: Karel J. Robot Chapter 6 Instructions That Repeat

Building a While Loop (cont’d)

Step 3 – make progress toward the goal (completion of the loop) within the loop.

Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem.

Page 17: Karel J. Robot Chapter 6 Instructions That Repeat

Remember to ITCH Initialize Test CHange

Page 18: Karel J. Robot Chapter 6 Instructions That Repeat

Avoid Infinite Loops In a for, while, or do-while loop, it

is possible for the test to never be false

When this happens, the loop continues infinitely

Depending on the compiler, application, and other circumstances, an error may occur or the app may crash

Page 19: Karel J. Robot Chapter 6 Instructions That Repeat

Example of Infinite loopwhile (facingNorth()) { pickBeeper(); move(); }

Page 20: Karel J. Robot Chapter 6 Instructions That Repeat

Examplepublic void clearCornerOfBeepers(){ while (nextToABeeper()) { pickBeeper(); }}

Page 21: Karel J. Robot Chapter 6 Instructions That Repeat

Examplepublic void goToBeeper(){ while (! nextToABeeper()) { move(); }}

Page 22: Karel J. Robot Chapter 6 Instructions That Repeat

Task:Karel is somewhere in the world facing

south. One beeper is on each corner between Karel’s current position and the southern boundary wall. There is no beeper on the corner on which Karel is currently standing. Write a method that will pick up all of the beepers. Name the method clearAllBeepersToThe Wall

Page 23: Karel J. Robot Chapter 6 Instructions That Repeat

Nesting Loops One loop can be nested inside of

another.

Good nesting

Bad nesting

Page 24: Karel J. Robot Chapter 6 Instructions That Repeat

Examplepublic void clearAllBeepersToTheWall()// corner can have more than one beeper{ while (frontIsClear()) { while (nextToABeeper()) { pickBeeper(); } move(); }}

Page 25: Karel J. Robot Chapter 6 Instructions That Repeat

Task: A robot named Karel is facing

south in the northwest corner of a room that has no doors or windows. Somewhere in the room, next to a wall, is a single beeper. Instruct Karel to find the beeper by writing a new method findBeeper.