An algorithm is a precise method, written as instructions, that solves a problem: it always comes to an end and gives the result, whatever the starting situation it is given.
Exemplos
Example 10.3 (Forward until the wall)
The robot stands on S, facing a wall somewhere ahead. The algorithm is:
repeat until the square ahead is a wall:
forward
Before each step, the robot checks the square ahead. Here it moves three times, then sees the wall and stops. With the wall one square further, the same algorithm would move four times: it works wherever the wall is.
Example 10.5 (A robot that finds its way)
The robot starts on S, facing right, on a grid with two walls. The algorithm is:
repeat until the robot is on the star:
if the square ahead is free then forward
otherwise turn left
A square is free if it is not a wall and is still on the grid: the edge of the grid counts as a wall. It moves forward three times; then the square ahead is a wall, so it turns left. It moves forward twice; the next square ahead is a wall again, so it turns left. Two more steps forward, and it is on the star: the loop stops, after 7 steps forward and 2 turns.