Karel the Robot
Programs




Karel's Basic Vocabulary

Karel has six built-in, or primitive, instructions. (By primitive, we mean they are elementary and basic.) Karel executes an instruction by performing the the action associated with that instruction. The six primitive instructions are:
TurnOn();
Karel powers up. This must be the very first instruction in every robot program.
Move();
Karel moves forward one block. He continues to face the same direction. To avoid damage Karel will not move forward when his path is blocked by a wall. Instead, Karel performs an error shutoff.
TurnLeft();
Karel turns by pivoting 90 degrees to the left. He remains on the same street corner.
PickBeeper();
Karel picks up exactly one beeper from the corner on which he is standing and puts it in his beeper-bag. If there is no beeper, Karel performs an error shutoff
PutBeeper();
Karel extracts a beeper from his beeper-bag and places it on the current street corner. If the beeper-bag is empty, Karel performs an error shutoff.
TurnOff();
Karel turns himself off and is incapable of executing any more instructions. This must be the last instruction executed in every robot program.

An Example Robot Program

To illustrate how these commands are packaged together to form a program for Karel to execute, examine the two situations below. The initial situation, on the left, shows Karel on street 2 avenue 2 facing East. At street 2 avenue 4 there is a beeper that we want Karel to transport to street 4 avenue 5. The final situation, on the right, shows the beeper moved to street 4 avenue 5. Karel has turned himself off one block further North at street 5 avenue 5 facing North. The following robot program correctly instructs Karel to perform this task.



// Beeper transport program. 

#include <karel.h>
using namespace std;

int main()
{
    TurnOn();
    Move();
    Move();
    PickBeeper();
    Move();
    TurnLeft();
    Move();
    Move();
    PutBeeper();
    Move();
    TurnOff();
}
Lines that start with // are comments to be read by humans.

The #include line tells the compiler, the program that translates the program to the left into a form that can be executed on the computer, that we are going to be using Karel instructions.

The using namespace std; statement tells the compiler to enable the standard namespace. For the time being, just accept that we will always have one occurrence of this statement at the beginning of a program.

The int main() and left curly brace ({) are used to package the Karel instructions. Everything up to the terminating right curly brace (}) is considered to be part of the Karel program.

Note that instruction statements are terminated by semicolons (;).




Note: The instruction TurnOn() should always appear exactly once in a program as the first executable statement. To indent the statements in the program, tab characters should be used instead of blanks; in a UNIX programming environment tabs are usually used to indent statements. The indentation used in this program is nice for us humans, but for Karel the program looks the same as

Programming Errors


Start of task

End of task




Erroneous Program

#include <karel.h>
using namespace std;    

int main()
{
    Move();
    PickBeeber();
    TurnLeft()
    PutBeeper();
    Move;
    }
}
When you first write a robot program, chances are that it will contain some errors. There are four types of programming errors:
Lexical Errors
The program contains an instruction or keyword that Karel does not know. These are akin to spelling errors in English.
Syntactic Errors
The rules for robot programming are violated, for example a missing semicolon or a keyword at the wrong place. These are akin to errors in grammar in English.
Execution Errors
Karel performs an error shutoff. This could happen because
  • the first command executed is not TurnOn()
  • he runs into a wall
  • a PickBeeper() command is executed on a beeper-less corner
  • a PutBeeper() command is executed with an empty beeper-bag
Intent (or Logic) Errors
Karel performs a task which is different from his assigned task.
Lexical and Syntactic errors are detected by the compiler, a program that translates the programs as written by us human programmers (we call this form the source code) into the machine code form that the computer can execute (we call this form the executable code). Hence we often call these first two types of errors compile-time errors. Execution and Intent (Logic) errors occur when the program is running; hence we often call these last two types of errors run-time errors.


The program on the left contains all of these four errors. Can you find and identify them? (The errors.)

















Prior Topic       |       Next Topic  

[MTSU]  | CS  | [Home] Return to Karel home page
(Credits and Copyrights)