What is sequencing and iteration?

Discover the basic ideas behind computer programs.

Show

Before we can go too deep into physical computing and creating your own projects, it’s a good idea to have a good grasp of the ideas behind computer programming.

This isn’t a full course on programming, there are lots of other courses you can do online and even on FutureLearn. xxx suggest course here? This gives you a basic understanding of the concepts behind programming. It will help you later on in the course when you’re creating.

All programming languages have the same concepts. Regardless of how they’re written, where they run and what they do, they all make use of these basic concepts

Sequence

Sequence is the order that commands go in. The order can really matter. Think of the instructions needed to make a cup of tea:

  1. Fill the kettle with water
  2. Turn the kettle on
  3. Put tea bag in cup
  4. Pour hot water into cup
  5. Stir
  6. Remove tea bag
  7. Add milk

If you did number 3 before 2 then that wouldn’t matter. However, if you did 2 after 1 then you won’t have enough water for your cup of tea.

This is a very simple example of sequence. We’ll highlight code examples in the actual programs later.

Selection

Select can be described as making a choice based on a criteria. Think of it as IF this THEN that.

It can add new options to your scenario: IF you take milk with your tea THEN add milk to your tea

Or it can create more efficient scenarios: IF the kettle has no water THEN fill the kettle with water

Rather than always filling the kettle with water, you’re asking a question first then making a decision based on the answer.

Selection is an important part of coding. It helps us choose different paths and react to situations.

Iteration

Iteration is repeating situations. There are many different types of iteration.

You can repeat a task a set number of times: Stir the tea 3 times

You can repeat a task until something happens: Stir the tea until the milk has mixed into the tea

We will highlight these different types of repetition in the upcoming projects.

Let’s look at our cup of tea task with Sequence, Selection and Iteration applied. We have the same number of steps but our process has become more precise (how long to stir for), efficient (we don’t fill the kettle if it has water in it already) and we have choice (milk or no milk)

  1. IF the kettle has no water THEN fill the kettle with water
  2. Turn the kettle on
  3. Put tea bag in cup
  4. Pour hot water into cup
  5. Remove tea bag
  6. IF you take milk THEN add milk
  7. Stir until the milk has mixed into the tea

Where else in your daily life can you see sequence, selection and iteration? These are not just programming concepts, these apply everywhere. Discuss in the comments a task you’ve noticed has one or all of these concepts. Any new ones you’ve just discovered?

What is sequencing and iteration?

There are many, many programming languages available that allow us to program computers to solve all kinds of problems. There are scripting languages, systems languages, web programming languages, dynamic languages, object-oriented languages, functional languages, and the list goes on and on. 

But did you know that all programming languages have 3 elements in common? Three very simple elements that give us the power to implement solutions to extremely complex problems. 

These 3 elements are: 

  •  Sequence 
  • Selection 
  • Iteration 

Sure, many programming languages have many other complex features. Some are ‘easy’ to learn and others more difficult to learn. 

In this post I’d like to talk about each one of these elements and build a very simple C++ program that uses all of them. C++ is one of those languages that is considered very difficult to learn because it is very complex. 

Let’s talk about each of these elements individually and we’ll write a simple C++ program along the way that uses all of them. We’ll keep this example simple and I’m sure you will be able to follow along. 

If you’d like to follow along by typing or copying/pasting the code below, you can do so without installing any software at all. Simply point your favorite browser to https://www.onlinegdb.com/  and select C++17 from the dropdown list at the upper right.

What is sequencing and iteration?

Then delete the text in the online editor window and type or copy/paste the code we’ll write along the way. 

What is sequencing and iteration?

When you are ready to run the program, simply click on the green Run button at the top of the screen. 

What is sequencing and iteration?

If you see any errors, then double check that you entered the code exactly as shown and try it again. Once the program runs, you will be able to enter data and see output at the bottom of the screen.

So, what are these 3 elements all about?

It’s actually very simple. In order to solve problems with any programming language, we write code that tells the computer what operations to execute and in what order. The order must be very specific – remember the computer is not very smart – it simply follows our instructions. These operations make up what is called an algorithm. Just a fancy word that describes a set of operations that solves a specific problem. You can think of this very much like a cooking recipe. If you follow the recipe exactly, you will end up with the produce of that recipe.

Sequence, Selection, and Iteration are the basic elements that we use to tell the computer what to do. The code will definitely look different depending on the programming language we use, but the algorithm will be the same.

So let’s describe these elements:

  • Sequence– the order we want the computer to execute the instructions we provide as programmers. For example, do this first, then do this, then do that, and so forth.
  • Selection– selecting which path of an algorithm to execute depending on some criteria. For example, if you passed a class in school, then we execute the operations that clap and cheer and play a song. But if you didn’t pass the class, then maybe we would say, “Better luck next time, hang in there!”
  • Iteration– looping or repeating. Many times, we want to be able to repeat a set of operations a specific number of times or until some condition occurs.

That’s it, these 3 super simple elements give us the ability to write programs that solve problems. When we put them together we can create programs that are very complex such as operating systems, game engines, compilers, anything! In fact, with just SequenceSelection, and Iteration we can implement any algorithm.

Read that again! Any algorithm! That’s a very powerful place to be!!

Alright, let’s write some C++ code together.

Sequence 

Let’s start with Sequence. Most programming languages simply execute instructions one after another as they are read – much like reading a recipe or a book. 

Here’s a simple C++ program that prompts the user to enter their age and then reads what they type in on the keyboard into a variable and then displays “Bye.” to the display console. 

#include <iostream> using namespace std; int main() { int age {0}; cout << "Enter your age: "; cin >> age; cout << "Bye."; }

All programming language have structure and syntax that we have to follow so that the program is legal, but that’s not important right now. So don’t worry about the details of the code, let’s focus on the big picture . The code on lines 7, 8, and 9 are what we are interested in. What’s important is that you can see the order or Sequence of the operations:

  1. We prompt the user to enter their age
  2. We read the integer they entered and store it in a variable named age
  3. We display Bye.

Run this program several times and enter different ages. Make sure you enter a whole number since that’s what the program expects.

Pretty boring right? Yes, but the computer is doing exactly what we are telling it to do.

That was pretty easy. But if we were only limited to Sequence our programs would be very limited indeed. We have to have a way to make decisions based on certain conditions.

Selection 

Now let’s modify this program so that it can decide whether a person can vote! We’ll assume that a person must be 18 years or older to vote. 

So, let’s see how Selection works. 

We’ll add logic so that if the age entered is 18 or greater then the program will display, “You can vote.” and if the age entered is less than 18 then the program will display, “Sorry, you can’t vote.”. 

See the selection, we are selecting which operations to execute depending on what the value of age is. Pretty simple and useful too! 

Let’s do it. 

#include <iostream> using namespace std; int main() { int age {0}; cout << "Enter your age: "; cin >> age; if (age >= 18) cout << "You can vote." << endl; else cout << "Sorry, you can't vote yet." << endl; cout << "Bye."; }

Notice that on line 10 we added an if statement. If the expression in the parentheses is true then we display, You can vote. 

But what if the person’s age is not greater than 18? That’s where the else on line 12 comes in. In this case we display, Sorry, you can’t vote yet.. We made a yes/no or true/false decision and we selected the operations to execute based on that decision. 

That was pretty easy. Notice how we combined Sequence and Selection to make our program more useful. 

Iteration 

But, suppose there are 3 voters and we would like to ask each of them what their age is. We could copy and paste the code 3 times, right? Sure, but if we had 1000 voters the program would grow very large and become very hard to follow and change. That’s where Iteration comes in. We can use a loop to iterate 3 times. Programming languages all allow Iteration and many provide multiple ways to iterate. Let’s do it using a very simple for loop in C++. 

So let’s rewrite the code one final time. 

#include <iostream> using namespace std; int main() { int age {0}; for (int count = 1; count <= 3; count++) { cout << "Enter your age: "; cin >> age; if (age >= 18) cout << "You can vote." << endl; else cout << "Sorry, you can't vote yet."<< endl; } cout << "Bye."; }

Notice that our original code is still there, we haven’t changed it. But we now added a new operation on line 7 that tells the program to count from 1 to 3 and execute everything from lines 9-15 that many times. 

So, now we will be asked to enter the ages for 3 voters and each time we’ll know whether a voter can vote or note. Pretty cool! 

Now our program is using Sequence to provide the order in which we execute our instructions. Selection to allow decisions to be made and Iteration to loop or repeat our instructions as many times as we need. 

Congratulations!

You have just learned the 3 elements that all programming languages must support and you wrote a simple program in one of the most complex programming languages used today, C++.

We can combine these 3 simple elements to implement any algorithm – now that’s power!

Of course, there is much more to programming. As our problems grow larger and more complex, we need other elements to help us deal with the complexity. Some of these elements are classes, methods, functions, threads, and many others.

But remember, the core elements of programming are the simple, Sequence, Selection, and Iteration elements that we learned about – that’s what it’s all about!

Ready to learn more?  Take a look at my Beginning C++ Programming – From Beginner to Beyond course.   It’s got close to 40 hours of video training and is aimed to make you a competent C++ programmer.  It’s one of the most popular courses of it’s kind on Udemy!