Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.

Similar presentations


Presentation on theme: "Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright."— Presentation transcript:

1 Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright ----------- Move ahead 5 streets Turn right Move ahead 5 streets Turn right Move ahead 5 streets Turn right Move ahead 5 streets Turn right Copyright © 2008 by Helene G. Kershner

2 Karel the Robot -- ITERATE beginning-of-program DEFINE-NEW-INSTRUCTION turnright AS BEGIN turnleft; END; DEFINE-NEW-INSTRUCTION move- ahead-5 AS BEGIN move; END; beginning-of-execution move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

3 Karel the Robot -- ITERATE The ITERATE command enables the programmer to have Karel repeat an instruction or set of instructions a fixed number of times. ITERATE TIMES The can actually be a group of instructions enclosed by a BEGIN and END Copyright © 2008 by Helene G. Kershner

4 Karel the Robot -- ITERATE Look at the definition for turnright DEFINE-NEW-INSTRUCTION turnright AS BEGIN turnleft; END; We could write this as follows: DEFINE-NEW-INSTRUCTION turnright AS BEGIN ITERATE 3 TIMES turnleft; END; Copyright © 2008 by Helene G. Kershner

5 Karel the Robot -- ITERATE Look at our program to have Karel walk around the block. The definition move-ahead-5 could be modified as follows: DEFINE-NEW-INSTRUCTION move-ahead-5 AS BEGIN ITERATE 5 TIMES move; END; Copyright © 2008 by Helene G. Kershner

6 Karel the Robot -- ITERATE Even withing the main program we repeat a lot of statements. beginning-of-execution move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; move-ahead-5; turnright; turnoff; Many of these instructions could be replaced by an ITERATE statement. beginning-of-execution ITERATE 4 TIMES BEGIN turnright; move-ahead-5; END; turnoff; Copyright © 2008 by Helene G. Kershner

7 Karel the Robot -- ITERATE Problem Statement: Karel has 5 beepers in his beeper bag. His task is to make sure that the five corners between 3rd Street and 3rd Avenue and 3rd Street and 7th Avenue all have one beepers. Two of these corners already have beepers on them. Copyright © 2008 by Helene G. Kershner

8 Karel the Robot -- ITERATE Revised Algorithm Pickup 5 beepers Move 2 avenue blocks If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Move 1 avenue block If no beeper, put one down Copyright © 2008 by Helene G. Kershner

9 Karel the Robot -- ITERATE beginning-of-program beginning-of-execution pickbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; move; IF not-next-to-a-beeper THEN putbeeper; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner

10 Karel the Robot -- ITERATE beginning-of-program beginning-of-execution ITERARE 5 TIMES pickbeeper; move; ITERATE 4 TIMES BEGIN IF not-next-to-a-beeper THEN putbeeper; move; END; IF not-next-to-a-beeper THEN putbeeper; turnoff; end-of-execution end-of-program Copyright © 2008 by Helene G. Kershner


Download ppt "Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright."

Similar presentations


Ads by Google