Secret Code

Arrays and Loops

Oliver Higgins

Issue 3, September 2017

Last month we looked at data types with variables. But what if we have a group of values that we want to store? That’s what arrays are for.

As we progress in programming, we are quickly presented with the daunting task of repetition and how to deal with it. After all, this is what computers are good at. In this article, we will discuss loops and arrays. Working with both of these we can automate many tedious and complicated tasks. So what comes first? The loop or the array? Depending on the loop, you need an array, and the best way to go through an array is with a loop - so the answer is both, concurrently. However, due to the our human inability to multi-task and read two columns at the same time, we will look at arrays first. We would recommend reading this article then re-reading it again, as the two subjects are intertwined.

To quickly recap, a variable is something we need to monitor and change. We define the variable and allocate data to go into it. This is then read by the computer and changed as the program progresses. Say, for example, we want to create a program that calculates two numbers. When we calculate two numbers, we allocate a container for the number, called A, and a container for our other number and call it B. The user enters data into the calculator. The calculator then gets the number from the A variable, performs the required math with B variable, and then returns the result. We now have a third number that we can output to the user. However, once your program becomes more complex, you will find that you will have similar types of data, or a lot of data that relates to the same information; in which case, defining an individual variable each time you need one, isn’t the best approach.

If we look back on a variable being a container that we can store items in, the array is a collection, group or list of those containers. For example, we can have six objects that are the same containers but hold different colours [1]. The array gives us the opportunity to simply tell the code which index our item is located in, and it will return that very item.


Here we have declared a series of six integer variables, which gives us six containers to use to store our information.

int x0 = 0 
int x1 = 0 
int x2 = 0 
int x3 = 0 
int x4 = 0 
int x5 = 0

However, we have to write it, access it, and every time we need to make a change it must be rewritten again, which becomes a very laborious task and one prone to user errors. What happens if there are 6 or 7 or 389,034 integers? It is impossible to maintain the code and also, you may not know how many variables we’ll need until runtime. What if it’s dynamic and the number needed changes? We want to be able to create a list of variables that we can move through, which are as big or small as we need. This is where the array comes in. If we are to look at our previous declarations on the six integers, we can do it much more efficiently using the following:

int x[6]  // this will give us six elements, but remember the count starts at 0 and goes to 5

Let’s take a closer look at this declaration. The first part is “int”. We are declaring a variable of type integer. Remember that all elements of the array must be of the same type; we cannot store a number in one element and a string in another. The next is the variable name, in this case, “x”. Finally, we have square brackets with a number in it. To access the element we simply specify the elements number that we need. For example, to read back the information in the first element we use:


This will give us the first element in the array. Remember, programmers start counting at 0, not 1. To access any element, we require, we just change the number:


This will give us the fourth element in the array. Now at this point, you will be forgiven for asking, “what difference does that have to the original set of variables we made?”. We still have to write down which one we want to access; so you are right, kind of; this is the segue we need to look at loops.

What if we could tell the system that we wanted it to do the same thing over and over again? How do we inform the system of what we need to be done, and how does it know where it is up to? This is the marriage between arrays and loops. For example, let’s say we have a class of 15 students and we need to store their final grades. We will be using the whole number, so we need to declare an array of integers to do this.

Want More?

Log in to unlock this article, or sign up now to receive two premium articles free!