---
title: "Instructions in Order"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 2
exercises: 10
source: https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 2 — Instructions in Order

Tom’s little brother Leo is hungry. Tom has to go out, so he writes the steps for making a jam sandwich on a piece of paper: “Spread jam on a slice of bread. Open the jar. Take two slices of bread. Put one slice on the other.” Leo follows the steps exactly as they are written. He tries to spread jam with the jar still closed. Then he opens the jar, and only then does he take the bread. When Tom comes back, the kitchen is a mess.

Every step on Tom’s paper was right. What went wrong was their order. A [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) is like Leo: it follows the steps it is given, exactly, in the order they are given.

**You already know.**

A [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) is a machine that follows [instructions](#def-g1-instructions-in-order-instruction) ([Chapter 1](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#ch-g1-what-is-a-computer)). People write the [instructions](#def-g1-instructions-in-order-instruction); the [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) carries them out, very fast, and nothing else.

## 2.1 A task as a list of steps

Many things we do every day are made of steps: making a sandwich, getting dressed, brushing our teeth, planting a seed. To tell somebody how to do such a task, we can write the steps one under the other.

**Definition 2.1 (Instruction).**

An *instruction* is one step that tells exactly what to do: “take two slices of bread”, “turn left”, “stop”.

A good [instruction](#def-g1-instructions-in-order-instruction) is short and clear. “Make the sandwich nicely” is not a good [instruction](#def-g1-instructions-in-order-instruction): Leo cannot know what “nicely” means. “Cut the sandwich in two” is clear.

**Example 2.2 (Brushing your teeth).**

1. Wet the toothbrush. 2. Put toothpaste on the toothbrush. 3. Brush your teeth for two minutes. 4. Rinse your mouth. 5. Rinse the toothbrush. Each line is one [instruction](#def-g1-instructions-in-order-instruction).

## 2.2 The order matters

**Definition 2.3 (Sequence of instructions).**

A *sequence of instructions* is a list of [instructions](#def-g1-instructions-in-order-instruction) to follow one after the other, from the first to the last, in the order in which they are written.

If two steps are swapped, the result can be wrong. You must put on your socks before your shoes. Tom’s sandwich went wrong because “open the jar” came after “spread jam”.

![The same five instructions in two orders. With steps 2 and 3 swapped, the jam cannot be spread: the jar is still closed.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/fig-b4fe3254fd91.svg)

*The same five [instructions](#def-g1-instructions-in-order-instruction) in two orders. With steps 2 and 3 swapped, the jam cannot be spread: the jar is still closed.*

Sometimes two steps can be swapped without any harm. You can put on your left glove first or your right glove first: both orders give two warm hands. Before you change the order of two steps, ask yourself: does the second step need the first one to be done?

## 2.3 Guiding a robot on a grid

In the classroom, Mia and Sami play with a small floor robot on a mat made of squares. The robot understands only three [instructions](#def-g1-instructions-in-order-instruction):

- **forward** ( $\uparrow$ ): move one square forward, in the direction it faces;
- **left** ( $\circlearrowleft$ ): turn a quarter turn to the left, on the spot, without moving to another square;
- **right** ( $\circlearrowright$ ): turn a quarter turn to the right, on the spot.

![A floor robot on a mat of squares. The children give it a sequence of instructions, then press start.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/img-536c612474af.jpg)

*A floor robot on a mat of squares. The children give it a [sequence of instructions](#def-g1-instructions-in-order-sequence), then press start.*

![The three instructions of the robot. The white dot shows where the robot faces. “Forward” moves it one square; “left” and “right” only turn it on its square.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/fig-0c10d9027f11.svg)

*The three [instructions](#def-g1-instructions-in-order-instruction) of the robot. The white dot shows where the robot faces. “Forward” moves it one square; “left” and “right” only turn it on its square.*

We write a sequence for the robot as a row of [instructions](#def-g1-instructions-in-order-instruction), read from left to right. The robot starts on the square marked S, facing to the right.

**Example 2.4 (Reaching the star).**

Mia writes the sequence

$$
\uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow\ \circlearrowright\ \uparrow
$$

(forward, forward, left, forward, forward, right, forward). The robot goes two squares to the right, turns to face up, goes up two squares, turns to face right again, and goes one more square. It stops on the star. The drawing below shows its path.

![The path of the robot for Mia’s sequence. It starts on S, facing to the right, and stops on the star.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/fig-f49fcb6712f4.svg)

*The path of the robot for Mia’s sequence. It starts on S, facing to the right, and stops on the star.*

**Remark 2.5 (Turning does not move).**

After “left” or “right” the robot is still on the same square: only the direction it faces has changed. That is why no square of the drawing carries the numbers 3 and 6: [instructions](#def-g1-instructions-in-order-instruction) 3 and 6 are turns.

## 2.4 Finding the wrong step

Sami wants the robot to reach the flag. He writes

$$
\uparrow\ \uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow\ \uparrow\ \uparrow
$$

but the robot stops far from the flag. Which [instruction](#def-g1-instructions-in-order-instruction) is wrong?

**Method 2.6 (Finding a wrong instruction).**

Put your finger on the start square, facing the right way. Read the sequence one [instruction](#def-g1-instructions-in-order-instruction) at a time and move your finger for each one, as the robot would. After each [instruction](#def-g1-instructions-in-order-instruction), check: is the finger still on a good path to the goal? The first [instruction](#def-g1-instructions-in-order-instruction) after which the answer is “no” is the wrong one. Change only that [instruction](#def-g1-instructions-in-order-instruction), then follow the whole sequence again from the start.

![Left: Sami’s robot turns left after three squares instead of four, so it goes up the wrong column. Right: with the turn moved one place later, the robot reaches the flag.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/fig-a8ea8f74d9ab.svg)

*Left: Sami’s robot turns left after three squares instead of four, so it goes up the wrong column. Right: with the turn moved one place later, the robot reaches the flag.*

**Example 2.7 (Fixing Sami’s sequence).**

With a finger: forward, forward, forward — the finger is three squares to the right of S. The flag is four squares to the right, so the next [instruction](#def-g1-instructions-in-order-instruction) should be “forward” again, not “left”. The fourth [instruction](#def-g1-instructions-in-order-instruction) is the wrong one. The fixed sequence is forward four times, left, then forward three times: the robot goes up the column of the flag and stops on it.

## 2.5 Exercises

Exercises 3 to 5 and 7 to 10 use the grid below; exercise 6 uses the grid of Mia’s robot, which has no walls. The robot always starts on S, facing to the right. The grey squares are walls: the robot cannot enter them.

![The grid of the exercises: start S, the squares A, B, C, D, and two walls.](https://one-course.com/images/onecourse/chapters/computer-science-1/g1-instructions-in-order/fig-49ea7c4f2bd5.svg)

*The grid of the exercises: start S, the squares A, B, C, D, and two walls.*

**Exercise 2.1 ★.**

Put these five steps of getting dressed in a good order: put on your shoes; put on your coat; put on your socks; put on your trousers; put on your underpants.

**Solution of Exercise 2.1.**

Underpants, trousers, socks, shoes, coat. (Socks may also come before the trousers; the shoes must come after the socks and after the trousers.)

**Exercise 2.2 ★.**

In the list of exercise 1, find two steps that can be swapped without any harm, and two steps that cannot.

**Solution of Exercise 2.2.**

Can be swapped: the trousers and the socks. Cannot be swapped: the socks and the shoes (shoes first, and the socks cannot go on).

**Exercise 2.3 ★.**

The robot follows $\uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow\ \uparrow$. On which letter does it stop?

**Solution of Exercise 2.3.**

Two squares to the right, a turn to face up, three squares up: the robot stops on C.

**Exercise 2.4 ★.**

Write a sequence that takes the robot from S to B.

**Solution of Exercise 2.4.**

For example: forward five times, left, forward four times: $\uparrow\uparrow\uparrow\uparrow\uparrow\ \circlearrowleft\ \uparrow\uparrow\uparrow\uparrow$ (10 [instructions](#def-g1-instructions-in-order-instruction)).

**Exercise 2.5 ★.**

Lina wants to reach A. She writes $\uparrow\ \uparrow\ \uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow$, and the robot stops one square to the right of A. Which [instruction](#def-g1-instructions-in-order-instruction) is wrong? Write the fixed sequence.

**Solution of Exercise 2.5.**

A is three squares to the right of S, so the fourth [instruction](#def-g1-instructions-in-order-instruction) must be “left”, not “forward”. One “forward” too many at the start: $\uparrow\ \uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow$.

**Exercise 2.6 ★.**

On the grid of Mia’s robot, with no walls, which of these sequences draws the path of its drawing (the path to the star)? (a) $\uparrow\ \uparrow\ \circlearrowright\ \uparrow\ \uparrow\ \circlearrowleft\ \uparrow$; (b) $\uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow\ \circlearrowright\ \uparrow$; (c) $\uparrow\ \circlearrowleft\ \uparrow\ \uparrow\ \circlearrowright\ \uparrow\ \uparrow$.

**Solution of Exercise 2.6.**

(b). Sequence (a) turns right after two squares and leaves the grid at the bottom. Sequence (c) also ends on the star, but by another path: it goes up from the second column, not from the third.

**Exercise 2.7 ★.**

The sequences $\uparrow\ \uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \uparrow$ and $\uparrow\ \uparrow\ \circlearrowleft\ \uparrow\ \circlearrowright\ \uparrow\ \circlearrowleft\ \uparrow$ both take the robot to the same square. Which square? How many [instructions](#def-g1-instructions-in-order-instruction) does each sequence have?

**Solution of Exercise 2.7.**

Both end on A. The first has 6 [instructions](#def-g1-instructions-in-order-instruction), the second 8.

**Exercise 2.8 ★★.**

Write the shortest sequence you can find that takes the robot from S to D without entering a wall. How many [instructions](#def-g1-instructions-in-order-instruction) does it have?

**Solution of Exercise 2.8.**

The walls block the two left squares of the middle row, so the robot must go up in the third column or farther right: $\uparrow\uparrow\ \circlearrowleft\ \uparrow\uparrow\uparrow\uparrow\ \circlearrowleft\ \uparrow\uparrow$, 10 [instructions](#def-g1-instructions-in-order-instruction). It needs at least 2 squares to the right, 4 up and 2 back to the left (8 forwards) and at least 2 turns, so 10 is the shortest.

**Exercise 2.9 ★★.**

Write a sequence that makes the robot leave S and come back to S.

**Solution of Exercise 2.9.**

For example $\uparrow\ \uparrow\ \circlearrowright\ \circlearrowright\ \uparrow\ \uparrow$ (two squares out, a half turn, two squares back), or $\uparrow\ \circlearrowleft\ \uparrow\ \circlearrowleft\ \uparrow\ \circlearrowleft\ \uparrow$ (around a small square).

**Exercise 2.10 ★★.**

The left [instruction](#def-g1-instructions-in-order-instruction) of the robot is broken: you may only use $\uparrow$ and $\circlearrowright$. Write a sequence that takes the robot from S to A.

**Solution of Exercise 2.10.**

Three right turns make the same turn as one left turn: $\uparrow\uparrow\uparrow\ \circlearrowright\circlearrowright\circlearrowright\ \uparrow\uparrow$.
