# Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.

## Presentation on theme: "Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when."— Presentation transcript:

Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when test = True

Karel – Making More Complex Decisions IF / THEN / ELSE Remember the possible options are: front-is-clearfront-is-blocked left-is-clearleft-is-blocked right-is-clearright-is-blocked next-to-a-beepernot-next-to-a-beeper any-beepers-in-beeper-bagno-beepers-in-beeper-bag

Karel – Making More Complex Decisions IF / THEN / ELSE Problem Statement: Karel has been told to place two beepers on each street corner between 1st street and 2nd avenue and 1st street and 10th avenue. This would be a very easy task except there was a beeper party last night and there are beepers scattered around on random street corners. Define Output: There will be two beepers on every corner on 1st street between 2nd avenue and 10th avenue. Define Input: Karel is next to 20 beepers. He is at the Origin facing east.

Karel – Making More Complex Decisions IF / THEN / ELSE

Initial Algorithm Pickup 18 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers Move If next to a beeper, put a beeper down Otherwise put down 2 beepers... turnoff

Karel – Making More Complex Decisions IF / THEN / ELSE Revised Algorithm Pickup 18 beepers Repeat 9 times Move If next to a beeper, put a beeper down Otherwise put down 2 beepers turnoff

Karel – Making More Complex Decisions IF / THEN / ELSE If next to a beeper, put a beeper down Otherwise put down 2 beepers Why did we solve the problem this way? 1. Karel cant really count 2. We could have him pick up the beeper and always put down 2 beepers: If next to a beeper, pickup beeper Put down 2 beepers 3. We want Karel to do as much work as possible.

Karel – Making More Complex Decisions IF / THEN / ELSE Anytime in our English algorithm (as opposed to Karels code) where the word otherwise or else makes sense we are being told to use the IF / THEN / ELSE command. If next to a beeper, put a beeper down Otherwise put down 2 beepers ------------------------ IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE (another word for Otherwise) BEGIN putbeeper; END;

Karel – Making More Complex Decisions IF / THEN / ELSE beginning-of-program beginning-of-execution ITERATE 18 TIMES pickbeeper; ITERATE 9 TIMES BEGIN move; IF next-to-a-beeper THEN BEGIN putbeeper; END ELSE BEGIN putbeeper; END;

Similar presentations