Kodeclik Blog

Sequences in Computer Science

A sequence is a list of instructions that must be executed strictly in the given order. All programming languages provide support to specify sequences of statements but you can find examples of sequences in real life.

Getting ready for school

When you wake up bright and shiny in the morning and need to get ready for school what do you do? You execute a sequence of steps, e.g., “take a bath”, “wear clothes”, “eat breakfast”, “board school bus”. While this might seem obvious to you, sequencing steps is not obvious to a computer and you have to explicitly tell the computer (through a programming language) the order in which these steps need to be executed. For instance, you cannot wear clothes first and then take a bath! Similarly, you cannot board the school bus and then eat your breakfast! As a result a sequence is often a numbered sequence of instructions lest there be no ambiguity to the order. For instance: “1. Take a bath. 2. Wear clothes. 3. Eat breakfast. 4. Board school bus.”

Baking recipes

Another example of a sequence can be seen in a baking recipe. The baking recipe is a very carefully designed sequence of instructions that detail not just the sequence of steps but also how many of each ingredient to use in each step, such as eggs, baking powder, baking soda, yeast, and sugar. The sequence will detail the order in which you need to add the ingredients, how much time you might need to wait (e.g., for the yeast to rise), and how long the mixture needs to be baked in the oven.

Driving instructions

When you use your car’s navigation system or Google Maps or one of those services to get from one location to another, the result is a sequence of steps. Again the order here is very important. The sequence will be something like: “1. Leave your driveway. 2. Take a right at Main Street. 3. Get onto the highway in the North direction. 4. Take Exit 34..” and so on. Note that you cannot flip or change the order. For instance, taking the second left followed by the third right is not the same as taking the third right followed by the second left! In this case the computer or website is giving instructions to you emphasizing the order.

Writing a computer program to add two numbers

If we aim to write a program to add two numbers, our program might look like:
Once again the sequence is very important. If we were to change the order, like so:
The program would make no sense! In fact if you submit such a program to an interpreter or compiler it will complain saying “I do not know what you are talking about!”. For instance, the first step gets the first number from the user. The second step instructs to add it to the second number which should make no sense since you are getting the second number only at the last step! Similarly, there is no result to print because the second step would not have gotten executed properly.
The moral of the story is that sequence is a very important concept in programming and computer science. Get your sequence right!
If you liked this blogpost, checkout our article on conditional statements.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2024. All rights reserved.