Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel J. Robot Chapter 6 Instructions That Repeat.

Similar presentations


Presentation on theme: "Karel J. Robot Chapter 6 Instructions That Repeat."— Presentation transcript:

1 Karel J. Robot Chapter 6 Instructions That Repeat

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

3 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

4 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

5 for Loops (Known number of iterations) General form: for ( ; ; ) { } 5

6 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

7 for Loops (cont.) Example: for (int i=1; i<=8; i++) { jumpHurdle (); } This causes Karel to jump hurdle 8 times 7

8 for Loops (cont.) Example: for (int i=1; i<=5; i++) { jumpHurdle (); } This causes Karel to jump hurdle 5 times 8

9 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. 9

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

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

12 while Loops (unknown number of iterations) General form: while ( ) { } Loop continues until test is false 12

13 while Loops (cont.) Example: while (frontIsClear ()) { move (); } Causes Karel to move continuously until there is a wall in front of it 13

14 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. 14

15 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

16 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

17 Remember to ITCH Initialize Test CHange 17

18 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

19 Example of Infinite loop while (facingNorth()) { pickBeeper(); move(); } 19

20 Example public void clearCornerOfBeepers() { while (nextToABeeper()) { pickBeeper(); } 20

21 Example public void goToBeeper() { while (! nextToABeeper()) { move(); } 21

22 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

23 Solution 23

24 Solution 24

25 Solution 25

26 Nesting Loops One loop can be nested inside of another. Good nestingBad nesting 26

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

28 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

29 Solution: Constructor and Main Method 29

30 Solution: toBeeper() method 30

31 Solution: The World 31


Download ppt "Karel J. Robot Chapter 6 Instructions That Repeat."

Similar presentations


Ads by Google