Sequence Control Structures

The sequence control structure arranges instructions into the linear order or sequence in which they are to be executed.

Selection Control Structures

Selection presents a choice between two processing options or steps, and as a result is sometimes called a binary selection. In this way, the selection follows the computer logic of ON/OFF, TRUE/ FALSE and YES/NO. one of the two options is presented and the choice will be made on the basis of whether or not the data meets the criteria set in the selection.

Selection may be divided into two broad types. As they are represented in pseudocode, the first is the IF…THEN situation, and the second is IF…..THEN….ELSE.

Where the selection is posed as a statement (eg the sun is shining), the logical flow (arrows) will be labeled True and False. Where the selection is presented as a question (eg is the sun shining?) the arrows will be labeled Yes and No.

Multiple Selections

In many cases it is necessary to make more than one simple choice between two options. Often a number of alternatives will be available for any given situation. In pseudocode, the end of all alternatives in marked by ENDIF.

Case Structure

A variation on multiple selections is the case structure. Since the case structure can be formed from a series of selection (IF THEN and IF….THEN….ELSE) controls, it is not considered a basic structure, but serves only to make the algorithm more compact and clearer. It simply groups together the related selections in a set. The case control matches the value of input variables to the action to be performed on the data. As a result, each input variable and its corresponding action are said to be a condition-action pair.

The case structure in pseudocode

CASEWHERE position is rewarded

The position is 1st

Give a trophy

The position is 2nd or 3rd

Give a medallion

The position is 4th to 10th

Give a blue ribbon

OTHERWISE give a certificate of participation

ENDCASE

Repetition control structures

Also called looping, repetition allows an instruction or set of instruction to be executed more than once. Loops are generally controlled by a test to determine if an action is to end or will continue to process the data. The control will be implemented either as a pre-test or a post-test

Pre-test

A pre-test is sometimes called a leading decision and is represented in pseudocode by the DO-WHILE control structure. In this form of repetition, the decision (criterion to be met) is placed at the beginning of the loop (ie before any of the processes in the loop are carried out).

Post-test

A post-test is also called a trailing decision, and in pseudocode it is the DO-UNTIL control structure. In this form of repetition, the decision is placed at the end of the loop (ie after the processes to be carried out in the loop). This means that the statements in the loop must be performed at least once.

In practice, the DO-While and DO-UNTIL methods may be used interchangeably. They are the main methods of performing a repetition.

There are two types of repetition:

·  Recursion

·  Iteration

Some programming languages will not support both forms of repetition. As a result, there is sometimes a need for the conversion of one into the other.

RECURSION

Recursion is a type of repetition that simply repeats the same procedure over and over again. As a result, the processing is unvaried with each pass through the loop. An infinite number of objects can be processes or defined by a single statement or procedure.

Recursion is a form of post-test or DO-UNTIL control structure.

ITERATION

Iteration is a form of looping in which the output from one stage forms the input of the next. It is represented by the pre-test repetition or DO-WHILE control structure. The program must count the number of times the process has been completed before it repeats the process. The example below shows the algorithms for calculating and displaying numbers 1 to 15.

Using EXIT to end a repetition

Apart from repetition being terminated when the test condition (either pre-test or post-test) is met, pseudocode also offers the possibility of terminating a loop by means of an EXIT statement. This is a test that takes place inside the loop, while the other forms of testing take place outside it. The EXIT statement can permit a loop to operate without a terminating condition, yet still end if a condition occurs that it is not set up to handle. It also allows the algorithm to terminate when one set of legitimate alternative occurs.

Counters

In many cases where repetitions occur there will be a need to count the number of times the program loops, so that it may be terminated when the correct number of repetitions has been made. This is done by means of a counter, in the form of a selection-control structure. Without a counter or some other means of terminating the repetition, the program will run continuously in an infinite loop. One useful application of an infinite loop is in digital watches, where they are used to count time.

In algorithms where a counter in used, it is placed before the process to be counted and is set to zero. In that way, the only things contributing to the count will be the repetitions of the process that is to be done and it will not include numbers from any other part of the machine.

Repetition using FOR- related statements

An alternative method of controlling the number of times a repetition occurs in some programming languages, such as BASIC, is by means of the FOR-related statements. The main type is the FOR…NEXT loop, but some languages also support FOR….UNTIL and FOR….WHILE statements. These carry out a procedure for a specified number of times. While they are actually composed of two parts, they are always used together and treated as a single operation.

Subprograms

Subprograms are also known as subroutines and are an instruction or a sequence of instructions which the program can jump to (or call up) when a particular event occurs. They are designed to be carried out (or called) more than once. The block of instructions contained in the subprogram is called a library function or library program. When written specifically by the programmer (rather than imported from a pre-existing library) they are called a user-defined library function.

Subprograms may be called into operation by the IF…THEN and IF….THEN…ELSE structures. They are identified in the algorithm by DO-WHILE, DO-UNTIL and FOR NEXT (or related) statements.

Subroutines help divide a long program into smaller, easier-to-handle parts. They occur in only one part of the main program, but can be called from almost any other part of it.

Checking the algorithm for errors

Checking the algorithm for errors begins at the defining stage and does not stop until the development of the program has been completed. The aim is to logically predict the outcome of the algorithm under given conditions, and then verify that the prediction is correct.

The process of checking an algorithm is called desk checking. The validity of the algorithm is checked by working through a written version of the algorithm using test data.

In desk checking an algorithm, each of the variables in the algorithm needs to be tracked. A record is kept using a test data table. Some algorithms contain variables that do not require the checker to set test data. Other algorithms require test data to be set in order to check the accuracy of the problem solution. Test data written to check an algorithm should include:

·  Legal, expected values such as boundary values and critical values in an algorithm

·  Illegal, expected values such as values outside the boundary of the problem

·  Illegal, unexpected values such as those due to keyboard errors or deliberate errors.

1