Karel J. Robot Chapter 6 Instructions That Repeat
Task: Have Karel run a mile long hurdle race Old Way public void runRace() { jumpHurdle(); jumpHurdle(); } 2
Old Way: public void runRace() { jumpHurdle(); jumpHurdle(); } New Way: public void runRace() { for (int i=1; i<=8; i++) { jumpHurdle(); } Task: Have Karel run a mile long hurdle race 3
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) 4
for Loops (Known number of iterations) General form: for ( ; ; ) { } 5
for Loops (cont.) sets a variable/identifier to a certain value (variable is usually count, i, or j) is a test that is evaluated each time the body is about to be executed (when false, loop is exited) changes the loop control variable (usually i++ or i-- ) 6
for Loops (cont.) Example: for (int i=1; i<=8; i++) { jumpHurdle (); } This causes Karel to jump hurdle 8 times 7
for Loops (cont.) Example: for (int i=1; i<=5; i++) { jumpHurdle (); } This causes Karel to jump hurdle 5 times 8
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 Instructions in body of loop are executed. 4. Increment statement executes. 5. Return to step Program proceeds to statements after loop. 9
Example: turnRight public void turnRight () { for (int i = 1; i <= 3; i++) { turnLeft(); } 10
Example: Harvest a row of beepers public void harvestOneRow() { for ( int i=1; i<=5; i++) { move(); pickBeeper(); } 11
while Loops (unknown number of iterations) General form: while ( ) { } Loop continues until test is false 12
while Loops (cont.) Example: while (frontIsClear ()) { move (); } Causes Karel to move continuously until there is a wall in front of it 13
while Loops (cont.) Flow of while loops 1. If test is true, proceed to step 2; if test is false, go to step Instructions in body of loop are executed. 3. Go to step Statements after loop are executed. 14
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 for the loop. 15
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. 16
Remember to ITCH Initialize Test CHange 17
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 18
Example of Infinite loop while (facingNorth()) { pickBeeper(); move(); } 19
Example public void clearCornerOfBeepers() { while (nextToABeeper()) { pickBeeper(); } 20
Example public void goToBeeper() { while (! nextToABeeper()) { move(); } 21
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 22
Solution 23
Solution 24
Solution 25
Nesting Loops One loop can be nested inside of another. Good nestingBad nesting 26
Solution public void clearAllBeepersToTheWall() // corner can have more than one beeper { while (frontIsClear()) { while (nextToABeeper()) { pickBeeper(); } move(); } 27
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. 28
Solution: Constructor and Main Method 29
Solution: toBeeper() method 30
Solution: The World 31