Skip to content

Sequence

Within a program, one of the main things you need to see is that everything runs in sequence.

At a low level, the computer executes each of its machine code instructions one at a time. This is central to the way the computer works and is therefore central to the way we need to think about its operation and giving it instructions of our own (computational thinking).

The computer’s processor maintains a program counter that is used to keep track of the instruction the computer is currently performing. Once it finishes the current instruction, the computer adds one to the program counter, moving it to the next instruction in the program. Some instructions will let you control which instruction is next, but this just lets you adjust the sequence — at its core, the computer is still performing one step and then the next.

The image below shows a visualisation of how the computer executes instructions in sequence. Instructions flow through the CPU in order, one at a time, similar to a data stream. Each instruction is very small, but the power of computing is that the CPU can execute millions of them each second.

Instructions flow through the computer in sequence

Keeping the simple idea of sequence in mind will be important throughout your programming journey and beyond. The computer is unintelligent and will follow the sequence of actions that you define. It will not interpret, adapt, or otherwise deviate from what you tell it to do.

Sequence — when, why, and how

Designing a program will require you to be able to identify a sequence of instructions that will get the computer to do what you want. This is the process of creating an algorithm — a finite sequence of instructions that a computer can use to perform a task or calculate an output. When creating algorithms you have to work within the constraints of the computer. The biggest constraint is that the instructions a computer can execute are unambiguous and require no intelligence to perform.

This design process is known as computational thinking. You need to think about the available instructions and building blocks, and to use them creatively to come up with a sequence of instructions.

In this chapter, you will learn to create the following building blocks:

You will be able to use the following building blocks that others have created:

  • Methods that can be called to perform an action or fetch/calculate a value.
  • Libraries that provide you with building blocks others have created.

You will be able to instruct the computer to:

By putting these together you will be able to build some small programs that show visualisations and calculate values. Once you have had practice with this, the next step in your journey will explore how we can make programs more dynamic by controlling the sequence of actions.

Sequence Up Close

Review the following images to see how sequences are executed within the computer.

There is no magic or intelligence here. Sequence is simply the computer adding one to a list that is tracking where the instructions are.

How small are these instructions?

One line of source code will generally have several instructions. Learning how this works is a big focus of this part of the guide. When you read and write source code, you need to be able to think through how the instructions will run. You don’t usually need to draw the steps out as we have here, but you need to be able to visualise them in your mind.

In general, this shouldn’t be too hard. Remember the following:

  • Instructions run in sequence. So read through the code line by line.
  • Calculated values (expressions) are evaluated before they are used. This process obeys standard mathematical rules (e.g. BOMDAS).
  • Study each different kind of instruction to know the steps involved in its sequence. Each kind of instruction will always have the same sequence.

With this in mind, you will be able to get started working with code that is all built upon this core concept of sequence.