Karel J Robot Chapter 6 Instructions That Repeat
Karel J. Robot Chapter 6 Instructions That Repeat
Task: Have Karel run a mile long hurdle race Old Way public void run. Race() { jump. Hurdle(); jump. Hurdle(); }
Task: Have Karel run a mile long hurdle race Old Way: public void run. Race() { jump. Hurdle(); jump. Hurdle(); } New Way: public void run. Race() { for (int i=1; i<=8; i++) { jump. Hurdle(); } }
Iteration (Loops) n n Loops repeat a set of instructions Two types of loops: n n 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)
for Loops (Known number of iterations) General form: for ( <initialize variable>; <test>; <increment>) { <instruction list> }
for Loops (cont. ) n n n <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--)
for Loops (cont. ) n n Example: for (int i=1; i<=8; i++) { jump. Hurdle (); } This causes Karel to jump hurdle 8 times
for Loops (cont. ) n n Example: for (int i=1; i<=5; i++) { jump. Hurdle (); } This causes Karel to jump hurdle 5 times
for Loops (cont. ) Flow of for loops: 1. 2. 3. 4. 5. 6. Initialization statement executes. If test is true, proceed to step 3; if test is false, go to step 6. Instructions in body of loop are executed. Increment statement executes. Return to step 2. Program proceeds to statements after loop.
Example: turn. Right public void turn. Right () { for (int i = 1; i <= 3; i++) { turn. Left(); } }
Example: Harvest a row of beepers public void harvest. One. Row() { for ( int i=1; i<=5; i++) { move(); pick. Beeper(); } }
while Loops n n (unknown number of iterations) General form: while ( <test> ) { <instruction list> } Loop continues until test is false
while Loops (cont. ) n n Example: while (front. Is. Clear ()) { move (); } Causes Karel to move continuously until there is a wall in front of it
while Loops (cont. ) Flow of while loops 1. 2. 3. 4. If test is true, proceed to step 2; if test is false, go to step 4. Instructions in body of loop are executed. Go to step 1. Statements after loop are executed.
Building a While Loop n n 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.
Building a While Loop (cont’d) n n 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.
Remember to ITCH n n n Initialize Test CHange
Avoid Infinite Loops n n n 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
Example of Infinite loop while (facing. North()) { pick. Beeper(); move(); }
Example public void clear. Corner. Of. Beepers() { while (next. To. ABeeper()) { pick. Beeper(); } }
Example public void go. To. Beeper() { while (! next. To. ABeeper()) { move(); } }
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 clear. All. Beepers. To. The Wall
Nesting Loops n One loop can be nested inside of another. Good nesting Bad nesting
Example public void clear. All. Beepers. To. The. Wall() // corner can have more than one beeper { while (front. Is. Clear()) { while (next. To. ABeeper()) { pick. Beeper(); } move(); } }
Task: n 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 find. Beeper.
- Slides: 25