The Four Basic Logic Patterns



After the processing requirements are known, the actual logic of the solution can be determined. In order to do this, it is necessary to know the basic logic patterns that the computer is able to execute. The power of the computer comes in large part through the programmer's ability to specify the sequence in which statements in a program are to be executed. However, the computer can execute only four basic logic patterns: the simple sequence, the selection pattern, the loop, and the branch. Pro-gramming languages may have more complicated statements, but they all are based on various combinations of these four patterns.

SIMPLE SEQUENCE

In a simple sequence the computer executes one statement after another in the order in which they are listed in the program. It is the easiest pattern to understand. The Figure below demonstrates the simple sequence pattern as it relates to the payroll example.

 

Simple Sequence Logic Pattern

SELECTION

The selection pattern requires that the computer make a choice. The choice it makes, however, is based not on personal preference but on pure logic. Each selection is made on the basis of the results of a comparison. The computer can determine if a given value is greater than, equal to, or less than another value; these are the only comparisons the computer is capable of making. Complex com-parisons are made by combining two or more simple comparisons. This process of requiring the computer to make a selection or choice is often referred to as conditional programming logic. The Figure below illustrates the selection pattern by demonstrating how the logic of the payroll example would consider overtime pay.
 

Selection Logic Pattern

LOOP

The loop or iterative pattern enables the programmer to instruct the computer to alter the normal next-sequential-instruction process and loop back to a previous statement in the program, so that a given sequence of statements can be performed as many times as needed. This is especially useful if the same sequence of statements is to be executed, say, for each employee in a payroll program; the programmer need not duplicate the sequence of statements for each set of employee data processed. The looping pattern is illustrated in the Figure below.

Loop Logic Pattern

BRANCH

The last and most controversial pattern is the branch (also called the GOTO), which is often used in combination with selection or looping (see Figure below). This pattern allows the programmer to skip statements in a program, leaving
them unexecuted.

Branch Logic Pattern

Branching is controversial for several reasons. If a program uses branches too often, the logic of the program becomes very difficult to follow. Such programs are difficult and time consuming for programmers to maintain and modify. Therefore, the use of the branch statement is strongly discouraged in most situations. When using most of the newer programming languages such as Pascal, Ada, and C, referred to as structured programming languages, there is very little need to use branch statements. Loops and selection patterns are used instead. These languages and their advantages will be discussed in later units.


Last Updated Jan 8/99