Presentation is loading. Please wait.

Presentation is loading. Please wait.

Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.

Similar presentations


Presentation on theme: "Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps."— Presentation transcript:

1 Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps

2 Karel’s Sensory Equipment Karel has three video cameras, a microphone, a compass, and tactile sensors to help him stay out of trouble. He can request information from his sensory equipment: frontIsClear, frontIsBlocked, nextToABeeper, etc. Each request will result in a true or false answer. Questions that can be answered as either true or false are called predicates.

3 Decision statements Karel can check predicates, i.e., you can ask Karel a question, by using what is called an IF construct. These constructs can be used wherever instructions are used in a program. In the C/C++ language, there are two variations of the IF statement –The simple IF –The compound IF-THEN-ELSE

4 The Simple If if (predicate) { } If the predicate is true the statements in the then clause are executed followed by the instructions immediately after the then clause, i.e, after the } If the predicate is false, then the statements in the then clause are not executed and we jump to the instruction immediately following the then clause, i.e, after the }

5 Karel’s Predicates frontIsClear leftIsClear rightIsClear facingNorth facingSouth facingEast facingWest nextToABeeper noBeepersInBeeperBag frontIsBlocked leftIsBlocked rightIsBlocked notFacingNorth notFacingSouth notFacingEast notFacingWest notNextToABeeper anyBeepersInBeeperBag

6 Examples the Simple IF if (nextToABeeper) { PickBeeper(); } if (frontIsClear) { Move(); TurnLeft(); } if ( frontIsClear ) { Move(); TurnLeft(); if (nextToABeeper() { PickBeeper(); } void FaceNorthIfFacingSouth() { if (facingSouth) { TurnLeft(); }

7 Compound If if(predicate) { } else { } If the predicate is true, the instructions in the then clause are executed then the instruction immediately after the then clause If the predicate is false, the instructions in the else clause are executed then the instruction immediately after the then clause

8 Examples of Compound IF if (frontIsClear) { Move(); } else { TurnLeft(); } void FaceNorthIfFacingSouthOrEast() { if (facingSouth) { TurnLeft(); } else { if ( facingEast ) { TurnLeft(); }

9 void Escape() { if (frontIsClear) { Move(); TurnLeft(); } else { TurnLeft(); Move(); } void GiveOrTake() { if (nextToABeeper) { if (anyBeepersInBeeperBag) { PutBeeper(); } else { PickBeeper(); }

10 Braces can be omitted when the then or else clause contains only one statement Original code: if (nextToABeeper) { PickBeeper(); } Move(); …with braces omitted if (nextToABeeper) PickBeeper(); Move();

11 Examples the Simple IF Revised if (nextToABeeper) PickBeeper(); if (frontIsClear) { Move(); TurnLeft(); } if ( frontIsClear ) { Move(); TurnLeft(); if (nextToABeeper() PickBeeper(); } void FaceNorthIfFacingSouth() { if (facingSouth) { TurnLeft(); }

12 Examples of Compound IF revised if (frontIsClear) Move(); else TurnLeft(); void FaceNorthIfFacingSouthOrEast() { if (facingSouth) { TurnLeft(); } else if ( facingEast ) TurnLeft(); }

13 void Escape() { if (frontIsClear) { Move(); TurnLeft(); } else { TurnLeft(); Move(); } void GiveOrTake() { if (nextToABeeper) if (anyBeepersInBeeperBag) PutBeeper(); else PickBeeper(); }


Download ppt "Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps."

Similar presentations


Ads by Google