Portfollio

Menu

Programming Fundamentals: Repetition

In the last article we looked at conditional statements, and how they allow for branching, the ability for a computer to decide what instructions to execute based on a set of conditions. The practice of evaluating conditions to dictate which instructions to execute or ignore, is only one application of conditions. Another way to use them is through repetition, the ability for a program to repeat instructions, in which they are repeated based on some condition.

A condition used in repetition, is used to determine how many times a computer should execute some instructions. In some cases, we know exactly how many times we want an instruction to repeat, and if it is only a few times, it is not difficult to type out the same instruction several times. Although when we want to repeat the execution of an instruction 10,000 times this would take a while to type out, and is an ideal situation where we would use repetition. Telling our program to execute the instructions under the condition that it executes them 10,000 times, which obviously saves us a whole lot of time!

In other cases, we may not know how many times we want instructions to repeat, but we know we need to execute them more than once. This maybe because we want to repeat them based on some user input, or based on the results of some other instructions, like a mathematical expression.

A good example program would be a video game. Games are very complicated programs, but the basic way in which they work involves using repetition. We start up our game, and run through the same instructions: asking for user input, updating the things that happen in the game such as characters and enemies moving or shooting, and finally displaying the game to the screen. We can play games for a matter of minutes or hours, and this straightforward process is repeated indefinitely through this time, until something occurs to cause game over. The exact condition in which game over occurs depends on the type of game, but this could be from losing all your lives, having no health left, or running out of time. Either way the game works by repeating a set of instructions until a game over condition evaluates to true, resulting in the game ending.

In terms of implementing repetition into our programs, like with conditional statements, all programming languages support repetition, using loops. There are several different types of loops but the two most common types are while, and for loops. A while loop looks like the following:

While (condition)

Execute instructions

Looks a lot like an if statement, except we replace the word if with while. What happens is that our program executes instructions based on the condition. The important thing to remember about a while loop is that the instructions won’t be executed a single time if the condition is not met. While loops are best used when we don’t know how many times we need to iterate (pass)through a loop, for example:

While (gameOver does not equal true)

Execute instructions

In the above example the instructions in the loop will be executed until the variable gameOver is changed to true, which could occur at any time. A for loop on the other hand looks like the following:

For (initialisation; condition; increment/decrement)

Execute instructions

A for loop seems slightly more complicated than a while loop, but it’s not too difficult to understand. For loops are split into three parts: initialisation, condition, and increment/decrement. In the first part this is where we initialise any variables we would like to use in the loop, usually a variable that stores an integer value. Second is our condition, this obviously decides how many times we iterate through the loop, repeating instructions. Typically, this will be some comparison such as x < 10, or x equals 10. The final part relates to how we alter the variable we initialised, whether we add or subtract some value, dependent on our needs. Below is an example of a for loop.

For (integer i = 0; i < 10; i = i + 2)

Execute instructions

In this example, we will execute our instructions 5 times. We start by initialising our i variable with the value 0 then we check if the value is less than 10, if it is then we add 2 to the variable then execute the instructions. This is repeated until i is no longer less than 10, which will occur after 5 iterations. It is best to use a for loop when we know exactly how many times we want our instructions to be repeated.

So far, we have looked at using loops as a way of allowing a computer to repeatedly execute instructions, but what types of instructions do we want to execute in a loop? Some examples include: adding or multiplying numbers, initialising variables, or even using loops to traverse through data structures.

The exact nature in which we traverse through a data structure depends on the programming language, and the type of data structure, but the general idea is that we can use a loop to traverse through all the data values we stored within a data structure. If we had a data structure that contained 100 different integer values, we could use a loop to traverse through the data structure, meaning we get access to each value, which we could then do something with, like change its value, or output it to the screen.

To conclude, repetition further enhances the capabilities of the programs we write by allowing us repeatedly execute instructions. Repeating instructions saves us from having to repeatedly type instructions in cases where we know the number of times we want an instruction to be executed. Additionally, they allow the creation of interactive applications such as games by allowing instructions to be executed several times unknown to the programmer. While and for loops are common implementations of repetition within programming languages, each tailoring to a specific situation, while loops best suited for situations where we do not know the number of times the instructions will be executed, and for loops reserved for times when we do know this information.

After reading this article you will be well on your way to understanding the fundamental principles required to write programs however there are still a few things left to learn. The next article will look at functions, what they are, and how they can be used to help us write better programs.