📘 CodingMarble Learn

Algorithm Analysis for Executors

An executor is a robot or program that does exactly the commands in its list. To analyse an algorithm we trace it: run every command by hand and note the state each time. A syntax error breaks the rules of the language (for example a misspelled command), so the executor stops. A logic error runs without complaint but gives the wrong result, and only tracing finds it. In computer graphics the origin (0, 0) is the top-left corner, x grows right and y grows down.

🎬 Step-by-step story

  1. An executor does exactly what its command list says. Our robot knows F, L and R.
  2. To analyse a program, trace it: do each command and write where the robot is.
  3. A syntax error is a broken rule, like the spelling FORWAD. The robot stops at that line.
  4. A logic error runs fine but ends in the wrong place. Only tracing finds it.
  5. Screen coordinates start at the top-left (0, 0). x goes right, y goes DOWN.
  6. Your turn: build your own program and try to reach the star in the fewest commands.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Why does the robot not just understand FORWAD?

A computer has no common sense. It only knows its exact list of commands. One wrong letter and the word is not in the list.

Which error is worse, syntax or logic?

Logic errors are harder, because nothing warns you. Syntax errors announce themselves.

Why trace by hand if I can just run the program?

Running shows only the end result. Tracing shows the first place where the program went wrong.

Why does y go down on a screen?

Screens are drawn row by row from the top, like reading a page. Row 0 is the first row, so the bigger the row number, the lower down it is.

Does a turn move the robot?

No. L and R change only the way it faces. Only F changes its position.

Can a program have both kinds of error?

Yes. First fix the syntax errors so that it can run, then trace to find the logic errors.

What is an executor?

An executor is anything that carries out commands: a robot, a drawing turtle, a calculator, or a computer. It has a fixed list of commands it understands, called its command set. It does exactly what is written. It never guesses what you meant.

Our robot lives on a grid. Its command set is: F (go forward one square), L (turn left on the spot), R (turn right on the spot). An algorithm is a list of these commands. The aim is to reach the star.

Tracing an algorithm

To analyse an algorithm means to find out what it really does. The best tool is tracing: play the executor yourself, one command at a time, and write down the state after each command (position and the way it faces).

CommandPositionFaces
start(1, 6)up
F(1, 5)up
F(1, 4)up
F(1, 3)up
R(1, 3)right

A turn changes only the direction. A forward step changes only the position. Trace tables like this are used by programmers every day.

Syntax errors

Syntax means the spelling and grammar rules of a language. A syntax error is a place where the program breaks those rules. Examples: a command with a wrong spelling (FORWAD), a missing bracket, a command the executor does not have.

Logic errors

A logic error (also called a bug in the thinking) means the program is written correctly, runs without any message, but does not do what you wanted. The robot turned after 2 steps instead of 3, so it stopped one square short.

How to debug: (1) run it, (2) compare the result with the goal, (3) trace to find the first wrong line, (4) fix only that line, (5) run again.

Coordinates in computer graphics

To tell a computer where a dot is, we use two numbers: (x, y). On a screen, the origin (0, 0) is the top-left corner.

The point (5, 2) is 5 across and 2 down. Our robot grid follows the same rule, so the squares in each row below the top have bigger y. When a drawing executor moves "up", its y gets smaller.

To change a screen y into a maths y on a screen with H rows (0 to H − 1): maths y = (H − 1) − screen y.

Try it yourself

In the 3D: in the last step, reach the star with exactly 7 commands. Then make a logic error on purpose, and then explain to a friend how you would find it.

At home: draw an 8 × 8 grid on paper. Write a program for a friend (F, L, R only) to walk from one square to another. Let the friend act as the robot with a pencil. Where it goes wrong, you have found a bug.

Key formulas and definitions

Worked examples

1. Trace: robot at (2, 5) facing up. Program: F F R F. Where does it end?

F: (2, 4). F: (2, 3). R: now faces right. F: (3, 3). It ends at (3, 3) facing right.

2. A program has the line "FORWRD". What kind of error is it and what happens?

A syntax error. FORWRD is not a command, so the executor stops at that line and reports it.

3. The robot should walk 4 squares up from (1, 6) but the program is F F F. What kind of error is it? How to fix it?

A logic error. The program runs but ends at (1, 3) instead of (1, 2). Fix: add one more F.

4. A dot is at x = 4, y = 3 on the screen. Describe where it is.

4 squares to the right of the left edge, and 3 squares down from the top edge.

5. A turtle at (3, 3) facing up moves F F. What is its new y?

Up on the screen means y gets smaller. 3 − 2 = 1. New position (3, 1).

6. The robot at (1, 6) facing up must reach (4, 3). Give the shortest program.

Three squares up and three squares right: F F F R F F F. That is 7 commands.

Common mistakes

Practice quiz

1. What is an executor?
2. The command FORWAD is:
3. A program runs with no message but gives a wrong result. This is a:
4. On a screen the origin (0, 0) is at the:
5. In screen coordinates, moving down makes y:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is an executor in informatics?

An executor is a robot or program that carries out the commands of its command set exactly as written, for example a robot on a grid or a drawing turtle.

What is the difference between a syntax error and a logic error?

A syntax error breaks the rules of the language, so the program cannot run that line. A logic error runs fine but gives a wrong result.

Where is the origin in computer graphics?

At the top-left corner of the screen. x grows to the right and y grows downwards.

Learn first

Learn next

Related lessons

All Computer Science lessons