Structured programming is one of the several different ways in which a programming language can be constructed. It was originally introduced as a means of getting away from the 'spaghetti' code that was used in the early days and to provide some means by which programmers could more easily follow code written by other programmers. Structured programming is a procedure oriented method of designing and coding a program.
A structured programming language does not allow just things to happen in any order within the code. There are a limited number of constructs that can be used within the code to define the execution flow. A structured program is one that only uses the constructs that are listed within this document. Most so called structured (or procedural) programming languages also permit constructs to be used that are not listed in this document. These additional constructs are to be avoided if a true structured program is to be produced.
A structured program may be written out using pseudo code prior to being translated into whatever programming language that the program is to be written in. This pseudo code forms part of the program specification and is readable by anyone who understands structured programming regardless of whether or not they know the specific language in which the program has been written.
Structured programming provides a number of constructs that are used to define the sequence in which the program statements are to be executed.
Statements within a structured program are normally executed in the same sequence as they are listed within source code. If a code fragment consists of three statements following one another then statement one will execute first, statement two second, and statement three last. To change from this straight consecutive execution sequence requires the use of one of the other structured programming constructs which are described below.
b = 5 + 2 * a
Statements may be blocked together. A block of statements may be substituted wherever a single statement is allowed.
The symbol or keyword used to indicate the start and end of each block differs depending on the programming language used.
A subroutine is a code segment that has been separated from the preceding and following code. A subroutine usually consists of a series of statements that perform a particular task. The task performed is usually identified by the name given to the subroutine.
Once a subroutine has been defined it can then be called from one or more places within the program. This allows a program to perform the same task a number of times without having to repeat the same code. A single call statement replaces (stands in for) all of the statements contained within the subroutine. Parameters can be passed to a subroutine which will supply the data required to perform the task and perhaps to return values for use by the subsequent processing.
A subroutine can either be compiled with (internal to) the calling program or separately (external).
call subroutine-1(var1, var2)
call output(a, b)
A function is similar to a subroutine except that a function always returns a value to the calling program. A function is usually called implicitly by embedding the function call into another statement in place of the returned value rather than having a separate call statement.
A function works in the same way as a subroutine except in the way that it is called. A function can be compiled internally or externally. Some programming languages also provide functions built into the language itself.
var2 = function-1(var1)
b = cube(a)
b = a * a * a
There are two types of branching statements. The if statement is used for conditional execution of a single statement or to select which of two statements is to be executed. The case statement (sometimes referred to as the select statement) allows for selection of one statement out of three or more that should be executed.
Statements can be executed conditionally by using an if statement. The if statement specifies a condition which gets tested when the if statement is executed. If the condition is true then the following statement is executed otherwise processing skips that statement.
Optionally a second statement can be attached to the if statement that will be executed if the condition is false.
if x > y
print "x is bigger than y"
print "x is not bigger than y"
A case or select statement allows for one of a number of statements to be executed depending on the value of a field. There is usually also an additional statement which is executed if none of the specified values is matched.
1: print "small"
2: print "medium"
3: print "large"
otherwise: print "unknown"
Loops allow for the same statement to be executed a number of times in succession. There are three different loop constructs that can be used depending on whether the number of repetitions is known and also (where the number of repetitions is not known and is dependent on a condition) whether the loop is allowed to be bypassed if the termination condition is met before the loop is first executed.
A for loop allows a statement to be executed a specified number of times.
The for loop begins with a loop control variable assigned a specific initial value. This control variable in then incremented (or decremented) by a specified amount each time around the loop until a specified terminating value is reached at which time the statement following the loop is then executed.
for (initial-value, final-value, increment)
for (a = 3, a > 12, a = a + 2)
This example will output 3 5 7 9 11.
A while loop allows a statement to be executed until a given condition is met. If the condition is met prior to executing the loop then the loop will not be executed. As soon as the condition is met, execution continues with the statement following the loop.
while not condition
while not end-of-file
An until loop also allows a statement to be executed until a given condition is met but the condition will not be tested until after the loop has been executed once. Once the condition is met the statement following the loop will be executed.
This article written by Stephen Chapman, Felgall Pty Ltd.