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:


PIC

Figure 2.1: *

Karel application in Manual mode.


Before we go further, recall the four major directions on the compass - North, South, East and West:


PIC

Figure 2.2: *

Four major directions on the compass.


The buttons change dynamically according to the direction which the robot is facing:


PIC *

Karel faces North.

PIC *

Karel faces East.

PIC *

Karel faces West.

PIC *

Karel faces South.


2.2. Steps and operations

On the bottom of the left panel you can see two icons that represent a computer (PIC) and a trace (PIC). 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:


PIC

Figure 2.4: *

Karel’s keyboard controls.


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:


PIC      PIC

Figure 2.5: *

When Karel faces North, his right is your right.


But when Karel faces South, then after turning right he will face West:


PIC      PIC

Figure 2.6: *

When Karel faces South, his right is your left.


This is the same skill that you need to correctly read maps.
picture

2.5. Programming mode

Now, let’s switch the Karel app to the Programming mode:


PIC

Figure 2.7: *

Karel application in 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:


Table 2.1: *
Control buttons in the Programming mode
PIC

Run the program

PIC

Step through the program

PIC

Stop the program

PIC

Erase the code

PIC

Restore the maze

PIC

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. picture

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:


PIC

Figure 2.8: *

Karel in his workshop.


This task can be solved using the following algorithm:


Algorithm 2.1: *
Algorithm
1:  Make two steps forward.
2:  Collect the gears.
3:  Turn around.
4:  Make three steps forward.


picture

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:

Program 2.1: Collect the gears and return to the home square!
1go 
2go 
3get 
4left 
5left 
6go 
7go 
8go

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:


PIC

Figure 2.9: *

Karel in his workshop.


Check that this program also solves the task!

Program 2.2: A different solution to the same task
1go 
2go 
3left 
4left 
5get 
6go 
7go 
8go

And here is yet another solution:

Program 2.3: Third solution to the same task
1go 
2go 
3left 
4get 
5left 
6go 
7go 
8go

And another solution:

Program 2.4: Fourth solution to the same task
1go 
2go 
3get 
4right 
5right 
6go 
7go 
8go

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

picture

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:


PIC

Figure 2.10: *

Karel in his workshop.



Program 2.5: A solution which requires many more operations
1go 
2go 
3get 
4right 
5go 
6right 
7go 
8go 
9go 
10go 
11right 
12go

This program solves the task, but it needs 12 operation while all the above programs only needed 8.
picture

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:


Algorithm 2.2: *
Algorithm
1:  Make three steps forward.
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!

Program 2.6: A program which does not work
1go 
2go 
3go 
4get 
5left 
6left 
7go 
8go 
9go

Try to find the logical error before reading further!
picture

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:

Program 2.7: Syntax error (code violates Karel syntax rule #1)
1go go 
2get 
3left 
4left 
5go 
6go 
7go

The following code violates Karel syntax rule #2 because of the indent on line 4:

Program 2.8: Syntax error (code violates Karel syntax rule #2)
1go 
2go 
3get 
4  left 
5left 
6go 
7go 
8go

Try to find three syntax errors in the following program!

Program 2.9: Karel program with three syntax errors
1go 
2go 
3got 
4r1ght 
5night 
6go 
7go 
8go
picture

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. picture

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.”


PIC

Figure 2.11: *

Rear Admiral Grace Murray Hopper.


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:


PIC

Figure 2.12: *

Karel in his workshop.


The robot can collect them using the following program:

1go 
2get 
3go 
4get 
5go 
6get 
7go 
8get

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:


PIC

Figure 2.13: *

The "bag" icon shows how many objects are in Karel’s bag.


When clicking on the icon, one can see all the objects. The most recently added ones are on top:


PIC

Figure 2.14: *

The contents of Karel’s bag.


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):


PIC

Figure 2.15: *

Karel carries four objects.


Let’s execute a program which makes Karel remove all objects from his bag and place them on the floor again:

1put 
2go 
3put 
4go 
5put 
6go 
7put 
8go

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:


PIC

Figure 2.16: *

Outcome of the program.


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:


PIC

Figure 2.17: *

Karel’s bag is now in FIFO mode.


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:


PIC

Figure 2.18: *

Objects are removed in the same order as they were inserted.


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.4. What command does Karel use to move one step forward?

A
forward
B
step
C
go
D
march

QUESTION 2.5. What command does Karel use to turn left?

A
leftturn
B
turnleft
C
goleft
D
left

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.8. In Manual mode, the Shift key represents the command

A
go
B
left
C
put
D
get

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 PIC 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 PIC 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.14. In the previous question, how many operations did Karel do?

A
6
B
7
C
8
D
9

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

Created on September 27, 2018 in Karel.
Add Comment
0 Comment(s)

Your Comment

By posting your comment, you agree to the privacy policy and terms of service.