Learn How to Think with Karel the Robot. Chapter 2. Basic Commands
Chapter 2
Basic Commands
In this chapter you will learn:
- How to control Karel in Manual mode using buttons and the keyboard.
- To see the world through the robot’s eyes.
- The difference between steps and operations.
- The difference between an algorithm and a program.
- How to write first Karel programs using basic commands.
- That most tasks have multiple solutions, and how to recognize the best one.
- The difference between logical and syntax errors, and what is debugging.
- About Karel’s bag, and the meaning of FILO and FIFO.
2.1. Manual mode
Let’s switch the Karel app in NCLab to Manual mode. Here, Karel can be guided by pressing buttons or certain keys on the keyboard. The keywords displayed on the buttons represent the five basic commands of the Karel language - go, left, right, get and put:
Before we go further, recall the four major directions on the compass - North, South, East and West:
The buttons change dynamically according to the direction which the robot is facing:
2.2. Steps and operations
On the bottom of the left panel you can see two icons that represent a computer () and a trace
(
). These are the counters of operations and steps, respectively. An operation is anything the
robot does - making one step forward, turning left or right, collecting an object, or placing
an object on the ground. The number of operations is always greater than or equal
to the number of steps. For example, when Karel makes a full 360-degree turn by
executing four times the command right, he will do four operations but make zero
steps.
2.3. Using keyboard controls
In Manual mode, the robot can also be guided using the keyboard. The corresponding keys are highlighted in the image below:
The up arrow corresponds to go, left arrow to left, right arrow to right. The Shift key corresponds to get and the Control (CTRL/CMD) key to put.
2.4. Seeing the world through the robot’s eyes
In order to guide the robot correctly, one needs to see the world through the robot’s eyes. For example, when the robot faces North, then after turning right he will face East:
But when Karel faces South, then after turning right he will face West:
This is the same skill that you need to correctly read maps.
2.5. Programming mode
Now, let’s switch the Karel app to the Programming mode:
Here, the robot can be guided by typing commands. The code cell is located in the left panel. The control buttons are explained in the following table:
![]() | Run the program | ![]() | Step through the program |
![]() | Stop the program | ![]() | Erase the code |
![]() | Restore the maze | ![]() | Close the Karel app |
2.6. Algorithm vs. program
Karel will always follow your commands exactly to the letter - no exceptions. If the robot does
something wrong, such as crashing into a wall, then most likely it was not his mistake but yours.
Your algorithm was wrong.
Algorithms are usually written using a common English language. For example, look at this maze where Karel needs to collect the gears and return to his home square:
This task can be solved using the following algorithm:
2: Collect the gears.
3: Turn around.
4: Make three steps forward.

A computer program (code) is an implementation of the given algorithm. The process of
translating an algorithm to a computer program is called coding. A computer program is formed
by one or more commands. Here is the Karel program corresponding to the above
algorithm:
2.7. Usually, a task has more than one solution
Almost every task has several solutions. Let’s stay with the previous one where Karel needs to collect the gears and return to his home square:
Check that this program also solves the task!
And here is yet another solution:
Can you find yet another solution?
Usually, one solution stands out by being the simplest or most elegant. But in this case, all the solutions are equally good.
2.8. How to recognize the best solution

Check out the following solution which also solves the above task. Can you visualize in your mind step by step what the robot is doing? Here is the maze again for reference:
This program solves the task, but it needs 12 operation while all the above programs only
needed 8.
2.9. Logical errors
Sometimes we think that we know how to solve a task, but our solution is wrong. Staying with the above example, let’s say that we come up with the following algorithm:
2: Collect the gears.
3: Turn around.
4: Make three steps forward.
Then we translate the algorithm to the following code, just to find out that it will not
work!
Try to find the logical error before reading further!
2.10. Syntax rules and syntax errors
Every programming language has its own syntax rules. These are important because the code is
parsed by a machine. It cannot be vague or arbitrary. For example, the first two syntax rules for
Karel are:
- (1)
- Always type one command per line.
- (2)
- Every command must start at the beginning of line.
Return for a moment to the codes above and verify that all of them satisfy these syntax rules! Of
course, programmers are human, and therefore they make all sorts of mistakes. For example the
program shown below contains a syntax error because two commands are written on the first
line:
The following code violates Karel syntax rule #2 because of the indent on line 4:
Try to find three syntax errors in the following program!

2.11. Debugging and where did the word "bug" come from
Mistakes of either kind (logical errors or syntax errors) are called bugs and the procedure
of eliminating them is called debugging. Depending on how careful one was while
preparing the algorithm and writing the program, debugging takes either a short
time or a long time. It does not happen often that a program works correctly right
away.
BTW, do you know why programming errors are called bugs? The first computer “bug” was a real bug — a moth. In 1947, Rear Admiral Grace Murray Hopper was working on the Harvard University Mark II Aiken Relay Calculator, an electromechanical computer. Grace Hopper tells the story: “Things were going badly; there was something wrong in one of the circuits of the long glass-enclosed computer. Finally, someone located the trouble spot and found a moth trapped between points at Relay #70 on Panel F. Using ordinary tweezers, we removed the two-inch moth. From then on, when anything went wrong with a computer, we said it had bugs in it.”
Grace Hopper’s legacy was an inspiring factor in the creation of the Grace Hopper Celebration of Women in Computing. Held yearly, this conference is designed to bring the research and career interests of women in computing to the forefront. The USS Hopper (DDG-70) is named in honor of Rear Admiral Grace Murray Hopper.
2.12. Karel’s bag
Karel has a bag where he stores all objects that he collects. Let’s return one more time to his workshop. This time there are four objects on the floor - a phone, watch, radio and a light bulb:
The robot can collect them using the following program:
In the next section we will show you how to shorten it to only three lines. For now, we will use it in this form. When the program finishes, Karel will have four objects in his bag:
When clicking on the icon, one can see all the objects. The most recently added ones are on top:
2.13. FILO (stack) and FIFO (queue)
Have you noticed the word FILO in the last image? It means that Karel’s bag is in FILO mode. FILO means stack in computer science. Let’s explain briefly what it does.
FILO stands for "First In Last Out" - objects which are inserted in some order will be removed in reverse order. In other words, the object which was inserted first will be removed last, and the object which was inserted most recently will be removed first. Recall that in the previous section, the four objects were collected in the following order (newest are on top):
Let’s execute a program which makes Karel remove all objects from his bag and place them on the floor again:
Since the bag is in FILO mode, objects are removed from the newest to the oldest. So, the light bulb is removed first and the phone last:
FIFO mode
FIFO means queue in computer science. Karel’s bag can be switched to FIFO mode in the Maze menu of the Designer. FIFO is an abbreviation of "First In First Out" - objects will be removed from the bag in the same order as they were inserted. Here is the contents of the bag again, with the newest items on top:
When the last program is executed again, the outcome will be different - the phone will be removed from the bag first and the light bulb last:
2.14. Karel is case-sensitive
The Karel language is influenced by Python and as such, it is case-sensitive. This means that the case of letters matters. For instance, the Karel command to make one step forward is go. Any other version such as Go, GO or gO will not work - typing them will result into an error message stating that you are attempting to use an unknown command.
2.15. Review questions
Friendly reminder - for every question either none, one, or several answers may be correct.
QUESTION 2.1. In Manual mode, Karel can be controlled using
- A
- buttons in the Karel app.
- B
- the keyboard.
- C
- written programs.
- D
- voice commands.
QUESTION 2.2. When Karel faces East, what direction will he face after turning right?
- A
- North
- B
- East
- C
- South
- D
- West
QUESTION 2.3. Karel faces West. What direction will he face after turning left three times?
- A
- North
- B
- East
- C
- South
- D
- West
QUESTION 2.6. What command does Karel use to place objects on the ground?
- A
- place
- B
- drop
- C
- insert
- D
- put
QUESTION 2.7. What command does Karel use to pick up objects from the ground?
- A
- collect
- B
- get
- C
- lift
- D
- pick
QUESTION 2.9. What is the equivalent to turning left?
- A
- Turning right two times.
- B
- Turning right three times.
- C
- Turning right four times.
- D
- Turning right five times.
QUESTION 2.10. What is the equivalent to turning right?
- A
- Turning left two times.
- B
- Turning left three times.
- C
- Turning right five times.
- D
- Turning left four times.
QUESTION 2.11. The computer icon represents
- A
- the number of operations done.
- B
- the number of steps made.
- C
- the number of lines in the program.
- D
- program duration in seconds.
QUESTION 2.12. In Programming mode the button is used to
- A
- run the program.
- B
- step through the program.
- C
- stop the program.
- D
- erase the program.
QUESTION 2.13. Karel makes three steps forward, turns back, and returns to the square where he started. How many steps did he make?
- A
- 4
- B
- 5
- C
- 6
- D
- 7
QUESTION 2.15. What statements are true about algorithms and programs?
- A
- An algorithm is typically written in plain English language.
- B
- A program is typically written in plain English language.
- C
- An algorithm is an implementation of a program.
- D
- A program is an algorithm rewritten into a concrete programming language.
QUESTION 2.16. What are the two basic syntax rules of the Karel language?
- A
- Type all commands using lowercase letters.
- B
- Always type one command per line.
- C
- Every command must start at the beginning of line.
- D
- Type all commands using uppercase letters.
QUESTION 2.17. What of the following errors are logical errors?
- A
- Making an indent where it should not be.
- B
- Making a left turn instead of a right turn.
- C
- Typing two commands on the same line.
- D
- Making three steps instead of two.
QUESTION 2.18. What of the following errors are syntax errors?
- A
- Misspelling a command, such as typing rlght instead of right.
- B
- Crashing Karel into a wall.
- C
- Typing 1O instead of 10.
- D
- Using a command which Karel does not know.
QUESTION 2.19. What was the first programming "bug"?
- A
- A mosquito.
- B
- A butterfly.
- C
- A moth.
- D
- A bee.
QUESTION 2.20. When Karel’s bag is in FILO mode (stack), then:
- A
- Object which was inserted first will be removed last.
- B
- Object which was inserted first will be removed first.
QUESTION 2.21. When Karel’s bag is in FIFO mode (queue), then:
- A
- Object which was inserted first will be removed last.
- B
- Object which was inserted first will be removed first.
Table of Contents
- About
- 1. Introduction
- 2. Basic Commands
- 3. Counting Loop
- 4. Conditions
- 5. Conditional Loop
- 6. Custom Commands
- 7. Variables
- 8. Functions
- 9. Text Strings
- 10. Testing Your Programs
- 11. Boolean Values, Variables, Expressions, and Functions
- 12. Randomness and Probability
- 13. Lists
- 14. Recursion
- 15. Advanced Applications
- Appendix A. Karel App in NCLab
- Appendix B. Self-Paced Karel Course in NCLab