---
title: "Algorithms: Repeat and Decide"
book: "Primary & Middle School Computer Science"
subject: computer-science
language: en
chapter: 10
exercises: 11
source: https://one-course.com/books/computer-science/1/en/chapter/10-algorithms-repeat-and-decide
license: CC-BY-NC-SA-4.0
credit: "One Computer Science Book, One Course (one-course.com)"
---

# Chapter 10 — Algorithms: Repeat and Decide

Lina, Tom and Noor play a card game. Cards are laid face up in a row, and the first to point at the largest card wins it. Tom looks at all the cards again and again, comparing them in pairs. Lina is much faster. She looks at each card only once, from left to right, and remembers just one thing: the largest card seen so far. When she reaches the end of the row, she knows. Lina is following a method, and a method that always works has a name.

![Which card has the most dots? Lina looks at each card once.](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/img-1b0fb34d117d.jpg)

*Which card has the most dots? Lina looks at each card once.*

**You already know.**

An [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) tells exactly one thing to do; a [sequence of instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-sequence) is carried out one after another, in order. Our floor robot understands *forward*, *turn left* and *turn right* ([Chapter 2](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#ch-g1-instructions-in-order)).

## 10.1 What an algorithm is

**Definition 10.1 (Algorithm).**

An *algorithm* is a precise method, written as [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction), that solves a problem: it always comes to an end and gives the result, whatever the starting situation it is given.

Lina’s method is an [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm): it works for a row of 5 cards or of 50, and it always ends, when the last card has been looked at. You already know other [algorithms](#def-g4-algorithms-repeat-and-decide-algorithm): adding two numbers in columns is one. Start with the units, add the digits, write the units of the sum and carry the tens to the next column, then do the same with each next column. The same steps work for any two numbers.

An [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) is written in words, or in drawings, so that a person or a machine can follow it without having to guess. Later in this course, we write [algorithms](#def-g4-algorithms-repeat-and-decide-algorithm) in a programming language, so that a [computer](https://one-course.com/books/computer-science/1/en/chapter/1-what-is-a-computer#def-g1-what-is-a-computer-computer) can carry them out.

## 10.2 Repeating

To make the robot walk around a square of 3 by 3 squares, we could write twelve [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction). But they are the same three [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) four times over: forward, forward, turn left. We can say it in one line.

**Definition 10.2 (Loop).**

A *loop* is a group of [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) that is carried out again and again: a given number of times (“repeat 4 times”), or until something happens (“repeat until the square ahead is a wall”).

![The loop “repeat 4 times: forward, forward, turn left”. The robot starts on S, facing right, walks around a square and comes back to S, facing right again.](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/fig-df60e4e8cbad.svg)

*The [loop](#def-g4-algorithms-repeat-and-decide-loop) “repeat 4 times: forward, forward, turn left”. The robot starts on S, facing right, walks around a square and comes back to S, facing right again.*

The second kind of [loop](#def-g4-algorithms-repeat-and-decide-loop) does not know in advance how many times it will run. It looks at the situation before each turn.

**Example 10.3 (Forward until the wall).**

The robot stands on S, facing a wall somewhere ahead. The [algorithm](#def-g4-algorithms-repeat-and-decide-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](#def-g4-algorithms-repeat-and-decide-algorithm) would move four times: it works wherever the wall is.

![“Repeat until the square ahead is a wall: forward”. The dark square is the wall.](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/fig-da7b9c701630.svg)

*“Repeat until the square ahead is a wall: forward”. The dark square is the wall.*

## 10.3 Deciding

Sometimes the next [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) depends on the situation: if the way is free, go on; otherwise, turn.

**Definition 10.4 (Condition).**

A *condition* is a question about the situation whose answer is yes or no. An [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) can *decide*: if the condition is true, it carries out some [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction); otherwise it carries out others.

“Is the square ahead free?”, “Is this card larger than the best one so far?” and “Is it raining?” are [conditions](#def-g4-algorithms-repeat-and-decide-condition). “Turn left” is not a [condition](#def-g4-algorithms-repeat-and-decide-condition): it is an [instruction](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction). “How many cards are there?” is not a [condition](#def-g4-algorithms-repeat-and-decide-condition) either: its answer is a number, not yes or no.

![A decision drawn as a diamond: the condition inside, one arrow for each answer. “If the square ahead is free, forward; otherwise, turn left.”](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/fig-82b91c4feefa.svg)

*A decision drawn as a diamond: the [condition](#def-g4-algorithms-repeat-and-decide-condition) inside, one arrow for each answer. “If the square ahead is free, forward; otherwise, turn left.”*

## 10.4 Following an algorithm by hand

To check an [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm), we follow it ourselves, step by step, exactly as written, and write down what happens. Nothing may be guessed.

**Example 10.5 (A robot that finds its way).**

The robot starts on S, facing right, on a grid with two walls. The [algorithm](#def-g4-algorithms-repeat-and-decide-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](#def-g4-algorithms-repeat-and-decide-loop) stops, after 7 steps forward and 2 turns.

![The robot’s path. At each dark wall it turns left; it stops on the star.](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/fig-d05112d3a5f7.svg)

*The robot’s path. At each dark wall it turns left; it stops on the star.*

**Method 10.6 (Finding the largest card).**

Take the first card as the best so far. Then, for each next card, from left to right: **if** it is larger than the best so far, **then** it becomes the best so far. When there is no card left, the best so far is the largest card.

![Finding the largest card of a row. The best so far is the first card, then changes twice (the outlined cards); the answer is 12. Each card is looked at once.](https://one-course.com/images/onecourse/chapters/computer-science-1/g4-algorithms-repeat-and-decide/fig-e4600d3cf709.svg)

*Finding the largest card of a row. The best so far is the first card, then changes twice (the outlined cards); the answer is 12. Each card is looked at once.*

Two [algorithms](#def-g4-algorithms-repeat-and-decide-algorithm) can reach the same result with different amounts of work. Tom compares every card with every other card: for 6 cards that makes 15 comparisons. Lina makes one comparison for each card after the first: 5 comparisons. For a row of 50 cards, Tom would need $1225$ comparisons and Lina 49. When two [algorithms](#def-g4-algorithms-repeat-and-decide-algorithm) give the same result, we choose the one that takes fewer steps.

## 10.5 Exercises

**Exercise 10.1 ★.**

The robot starts in the bottom left square of an empty grid, facing right. Follow “repeat 3 times: forward, forward, turn left”. Draw its path. On which square does it stop, and which way does it face?

**Solution of Exercise 10.1.**

It walks 2 squares right, 2 squares up, then 2 squares left: three sides of a square. It stops on the third square up of the leftmost column (2 squares above the start), facing down.

**Exercise 10.2 ★.**

Write a [loop](#def-g4-algorithms-repeat-and-decide-loop) that makes the robot walk around a rectangle 4 squares wide and 2 squares high, and come back to where it started.

**Solution of Exercise 10.2.**

> **repeat** 2 times:
>     forward, forward, forward, turn left, forward, turn left

The robot walks along a row of 4 squares, goes up one, comes back along the row above, and goes down one: it is back on its start square, facing right.

**Exercise 10.3 ★.**

Find the largest card of the row 5, 11, 2, 8, 14, 6, 9 with Lina’s method. Write the table “card looked at / best so far”.

**Solution of Exercise 10.3.**

| card looked at | 5 | 11 | 2 | 8 | 14 | 6 | 9 |
| --- | --- | --- | --- | --- | --- | --- | --- |
| best so far | 5 | 11 | 11 | 11 | 14 | 14 | 14 |

The largest card is 14.

**Exercise 10.4 ★.**

Change one word of Lina’s method so that it finds the smallest card. What is the smallest card of the row of the previous exercise?

**Solution of Exercise 10.4.**

Replace “larger” by “smaller” (and read “best so far” as “smallest so far”). The smallest card is 2.

**Exercise 10.5 ★.**

Which are [conditions](#def-g4-algorithms-repeat-and-decide-condition)? (a) Is the square ahead a wall? (b) Go forward. (c) Is the card red? (d) How many dots are on the card? (e) Is the robot on the star? (f) Turn left.

**Solution of Exercise 10.5.**

(a), (c) and (e) are [conditions](#def-g4-algorithms-repeat-and-decide-condition): their answer is yes or no. (b) and (f) are [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction). (d) is a question, but its answer is a number, so it is not a [condition](#def-g4-algorithms-repeat-and-decide-condition).

**Exercise 10.6 ★.**

The robot starts in the bottom left square, facing right; the third square of the bottom row is a wall. Follow “repeat 4 times: if the square ahead is free then forward, otherwise turn left”. Where does the robot stop?

**Solution of Exercise 10.6.**

Step 1: the second square is free, forward. Step 2: the square ahead is the wall, turn left (now facing up). Steps 3 and 4: forward, forward. The robot stops on the second square of the third row, facing up.

**Exercise 10.7 ★.**

Add $56 + 27$ in columns, and write each step of the [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) in a sentence.

**Solution of Exercise 10.7.**

Units: $6 + 7 = 13$; write 3 and carry 1 to the tens. Tens: $5 + 2 + 1 = 8$; write 8. There is no column left: the sum is 83.

**Exercise 10.8 ★.**

The robot starts on S, facing right. Which [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program) draws the path of the square in the drawing of the [loop](#def-g4-algorithms-repeat-and-decide-loop): “repeat 4 times: forward, forward, turn left”, or “repeat 4 times: forward, forward, turn right”? Where does the other one go?

**Solution of Exercise 10.8.**

“Forward, forward, turn left”: after the first two steps it turns up, so it walks around the square above and to the right of S. With “turn right”, it would turn down after two steps and walk around a square below S, which is off this grid.

**Exercise 10.9 ★★.**

The robot starts in the bottom left square, facing right, and must reach the square 2 to the right and 2 up. Count the [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction) of each [program](https://one-course.com/books/computer-science/1/en/chapter/9-programs-and-the-operating-system#def-g4-programs-and-operating-system-program), and check that each one reaches the goal. Which is the shortest? (a) forward, forward, turn left, forward, forward. (b) turn left, forward, forward, turn right, forward, forward. (c) forward, forward, forward, turn left, forward, forward, turn left, forward.

**Solution of Exercise 10.9.**

(a) 5 [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction): 2 right, turn, 2 up. (b) 6 [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction): 2 up, turn, 2 right. (c) 8 [instructions](https://one-course.com/books/computer-science/1/en/chapter/2-instructions-in-order#def-g1-instructions-in-order-instruction): 3 right, 2 up, 1 left. All three reach the goal; (a) is the shortest.

**Exercise 10.10 ★★.**

On an empty grid of 4 by 4 squares, the robot starts in the bottom left square, facing right; the star is on the second square of the second row. The [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) is the one of the robot that finds its way. Follow it for a while. Does it ever stop? What is missing for this to be an [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm)?

**Solution of Exercise 10.10.**

The robot walks along the edge of the grid: right to the corner, up, left, down, and back to its start square, turning left at each corner. It never passes through the star, which is inside, so it goes round and round forever. An [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) must always come to an end: this one needs, for example, a way to reach squares that are not on the edge, or a rule that stops it when it is back where it started.

**Exercise 10.11 ★★.**

Write an [algorithm](#def-g4-algorithms-repeat-and-decide-algorithm) that counts the red cards of a row of cards. Use a [loop](#def-g4-algorithms-repeat-and-decide-loop) and a decision.

**Solution of Exercise 10.11.**

For example:

> Start with a count of 0.
> **repeat** for each card of the row, from left to right:
>     **if** the card is red **then** add 1 to the count
> When there is no card left, the count is the number of red cards.
