What is Sequencing in Coding?

Put simply, you can think of the concept of sequencing in coding as the order in which an algorithm’s instructions execute. Without correct sequencing, you can expect any program you write to either perform incorrectly or crash altogether.

My name is Jamie Smith, and I have learned first-hand how important good sequencing is. In two decades of writing code for fun, I’ve sometimes caused myself serious headaches due to poor sequencing in my own programs!

Today, I’ll break down the concept of sequencing, why it plays such an important role in software development, and provide a few code samples for maximum clarity.

What is Sequence Structure in Programming?

As mentioned above, sequence structure is the practice of ordering an algorithm’s instructions in order to achieve a desired result. 

This can be anything, from a game loop, to a mathematical equation, to a file operation; essentially, any situation where it’s imperative for instructions to be executed in the right sequence (which tends to be the vast majority of computer programs, if not all!).

Sequencing forms part of the so-called “building blocks” of computer algorithms, along with selection and iteration.

Why is Sequencing Important?

Sequencing is important because computers are – generally speaking – dumb! If you tell a person to perform a series of tasks that appear to be in the wrong order, they’ll most likely realize that there was an issue before, during, or after completing them.

A computer, on the other hand, is going to execute the orders as it reads them whether they make sense or not! Although coding tools are becoming increasingly helpful, it’s still much harder for your development environment to understand exactly what it is you’re trying to do. 

Oftentimes, with sequencing mistakes, your code will appear to work and the computer won’t even throw up an error; instead, it will only become obvious that there’s an issue when you’re looking at the final performance (or output) of your program.

How to Implement Sequencing

The best way to really understand sequencing is to experiment with it. There’s a good chance you’ll write code you could swear was perfect, but which isn’t performing the way you’d expected it to. 

It’s very likely that these issues are caused by incorrect sequencing, but let’s go over a few examples which illustrate how this kind of problem can arise.

Sequence Algorithm Example

The below program is written in pseudo-code for readability, but the logic demonstrated will apply to any “real” programming language, too. The purpose of our code is to perform the following set of instructions ten times:

  1. Write the current time to a text-file
  2. Enter a newline character
  3. Wait 2 seconds

We might try to do this as follows:

Create text file “time.txt”
Create Variable ‘Time’
Start iteration loop from 1 to 10
    Write Variable ‘Time’ to “time.txt”
    Add a newline
    Wait 2 seconds
    Variable ‘Time’ = Current Time
Close text file “time.txt”

The problem with the above code, as you may have already figured out yourself, is that we have sequenced it incorrectly. Between the second and tenth iterations, it may still work as expected, but the first one is going to be problematic as we haven’t yet assigned a value to our ‘Time’ variable.

To fix the sequencing, you would move “Variable ‘Time’ = Current Time” to directly before “Write Variable ‘Time’ to “time.txt””. 

Example Sequence in Programming

Now, let’s fix the above algorithm (by moving the line where the ‘Time’ variable is updated) using some real-world code. In this example, we’re going to use Python.

#!/usr/bin/python3
import time

time_file = open("time.txt","w")
for x in range(10):
    var_time=time.asctime(time.localtime())
    time_file.write(var_time)
    time_file.write("\n")
    time.sleep(2)
time_file.close()
print("time.txt has now been closed.")

Now, whenever our loop executes, var_time should always have a new value (the current local time) before it’s written to our text file. We have fixed the sequencing, and we can now rest assured that our code will work as we had originally intended it to.

Closing Thoughts

The concept of sequencing in coding is fairly straightforward, but it will be absolutely integral to your practice. 

Being able to not only write working code – but also to visually comprehend the flow of data in your program by reading each line sequentially – is a skill that any programmer worth their salt must develop.

We hope that this article has helped you to understand how sequencing plays an important role in coding. We always love to hear feedback, so please feel free to share your opinions and any other topics you’d like us to write about in the future.

Leave a Reply

Your email address will not be published.