Download presentation
Presentation is loading. Please wait.
Published byValentina Dayley Modified over 10 years ago
1
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
2
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
3
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.
4
Karel – Making More Complex Decisions IF / THEN / ELSE
5
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
6
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
7
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.
8
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;
9
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
© 2025 SlidePlayer.com. Inc.
All rights reserved.