Structuring of Control Flow

Structuring of Control Flow:

Except for the most trivial examples, programs are highly complex systems of logic. The ability to design loop operations and to take alternative courses of action depending on a condition can make a program very complex. This is because, quite often, loops are used within alternative paths or loops and alternative paths are used within loops or other alternative paths. The large size of a program and the interconnections between its various parts through the transfer of control also increase the complexity of a program.

The idea behind the structuring of a control flow is to keep this complexity under control so that the design and understanding of the program logic becomes easier. It has been demonstrated that a structured program can be completely developed using three basic forms of program structure, viz., (i) sequence structure, (ii) decision or selection structure, and (iii) loop structure. One may encounter these as special terms which are written: SEQUENCE, IFTHENELSE, DOWHILE.

(1) Sequence Structure- The SEQUENCE structure indicates the sequential flow of program logic. Each block may stand for a statement; it may stand for a whole module or even a collection of modules. In a SEQUENCE structure, the statements are executed one after the other in the order in which they are written. The flowchart of such a structure is shown below. The flow of control is such that it performs block A first, then block B, and so on, from top to bottom in the same direction. There is only one entry point and one exit point for the structure and there is no complexity. The flow is in one direction and straight.

flowchart of sequence structure

(2) Decision Structure- The IFTHENELSE structure indicates a conditional program flow. The program takes one path or another, depending on whether a condition, often referred to as the predicate, is true or false. The program on entering the test box C shown below makes a decision to go through either of the two paths, A or B. After performing one of the two paths, the control returns to a single exit point. It can be seen that this structure also has one entry point and one exit point, even though there are multiple exit paths inside the structure.

flow chart of the ifthenelse structure

(3) Loop Structure- The DOWHILE structure provides for a looping operation, i.e., repetitive execution of a program segment. A loop is not infinite if it is correct. The predicate tests for a condition. If the condition holds, we exit from the loop; if not, we execute the instruction. The flowchart of this structure is shown below. Whether the operations are to be repeated or stopped is decided in the decision box.

flowchart of the dowhile structure

Any program can be written by using these three basic building blocks. In complex programs, one structure may be embedded in the other. For example, in the below figure, a nested structure is shown. When selection between the two available paths is made in decision block C1, the control enters either a loop structure on the left or a sequence structure on the right. After performing any one of the two paths, the control returns to a single exit point. In a similar manner, any number of structures can be embedded, provided the combination as a whole has one entry point and one exit point.

flowchart of the nested structure

Need for Networking in Computer
Control flow– Wikipedia

Leave a Reply