Karels Sensory Equipment - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Karels Sensory Equipment

Description:

Karel has three video cameras, a microphone, a compass, and tactile sensors to ... These constructs can be used wherever instructions are used in a program. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 14
Provided by: judyhankin
Category:

less

Transcript and Presenter's Notes

Title: Karels Sensory Equipment


1
Karels Sensory Equipment
  • This PPT originated with Dr. Roland Untch
  • Modifications have been made by Dr. Cripps

2
Karels 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)
  • ltthen-clause statement(s)gt
  • 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
Karels 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()
  • TurnLeft()

7
Compound If
  • if(predicate)
  • ltthen-clause statement(s)gt
  • else
  • ltelse-clause statement(s)gt
  • 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
  • void FaceNorthIfFacingSouthOrEast()
  • if (facingSouth)
  • TurnLeft()
  • TurnLeft()
  • else
  • if ( facingEast )
  • TurnLeft()
  • if (frontIsClear)
  • Move()
  • else
  • 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()
  • TurnLeft()

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

13
  • void Escape()
  • if (frontIsClear)
  • Move()
  • TurnLeft()
  • else
  • TurnLeft()
  • Move()
  • void GiveOrTake()
  • if (nextToABeeper)
  • if (anyBeepersInBeeperBag)
  • PutBeeper()
  • else
  • PickBeeper()
Write a Comment
User Comments (0)
About PowerShow.com