Back

Programming constructs

Introduction
When you look at any high-level language, you can identify the features in the language that make it so powerful. Instructions in a program flow one after another in a sequence. However, there are instructions available that will allow blocks of code to be repeated many times. There are also instructions that allow decisions to be made about which code from a selection of code will be executed. The specific instructions provided by a particular language that allow code to be repeated, or code to be selected from a choice of code, are known as control structures. If we also include the way that instructions flow one after the other, we can say that there are three types of programming construct we need to know about. These we will call:

    1. Sequence.
    2. Selection. (There are two types of selection we need to know about.)
    3. Iteration, also called repetition. (There are three types of iteration we need to know about.)

Sequence
There isn't a lot to say about the programming construction 'sequence'. It simply describes one instruction following on from the next instruction. An example of this, in pseudo-code is:

INITIALISE Variables
WRITE ‘Please enter the name of student’
INPUT Name
WRITE ‘Please enter the exam mark of student’
INPUT ExamMark
PRINT Name, ExamMark

Selection
A selection construct looks at the data held in a variable (CASE), or tests data in a variable for TRUE of FALSE (using an IF statement). Depending on what the variable holds, or whether the test is true or false, different code will be executed. In this way, we can alter the program flow and execution. Some blocks of code may never get executed at all whilst others will get executed. We will look at selection in a lot more detail 3.2.2.

Iteration
Iterative constructs (also known as repetitive constructs) repeat the same block of code a number of times using either a FOR loop, a WHILE loop or a REPEAT - UNTIL loop. How many times depends either on the type of construct and in the case of WHILE and REPEAT, whether the result of a test is true or false. These types of constructs result in smaller programs because, for example, if you need to run a block of code 10 times, you only actually have to write it down once (and use an iterative construct). We will look at these types of constructs in a lot more detail 3.2.2.

Summary
To summarise, there are three programming constructs you need to know about, and within selection and iteration, there are different kinds of constructs you could use. We can pull all this together with a diagram and we will look at selection and iteration in more detail in the next few sections.

constructs

 

Back