Geek Logbook

Tech sea log book

The elements of programming style: Control Structure

Chapter 3: Control Structure

A computer program is shaped by its data representation and the statements that determine its flow of control. These define the structure of a program. There is no sharp distinction between expression and organization. 

The control structures of a language provide the framework of a program. These include decision-making with IF and ELSE; looping with DO and WHILE; statement grouping; and procedures or subroutines and functions.

  • Use DO-END and indenting to delimit groups of statements
  • Use IF-ELSE to emphasize that only one of two actions is to be performed
  • Use DO and DO-WHile to emphasize the presence of loops
  • Make your programs read from top to bottom
  • Use IF … ELSE IF … ELSE IF … ELSE … to implement multi-way branches
  • Use the fundamental control flow constructs.
  • Write first in an easy-to-understand pseudo-language; then translate into whatever language you have to use

When a program is well-structured, its layout can be made clean. For instance, programs that avoid labels and undisciplined branches should use indentation to emphasize the logical structure. (This program was originally displayed with vertical bars joining IF’s with their corresponding ELSE’s.) But indentation is no substitute for organization; tasteful formatting and top-to-bottom flow is no guarantee that the code cannot be improved. 

Look at all the repetitions. 

  • Avoid THEN-IF and null ELSE
  • Avoid ELSE GOTO and ELSE RETURN

The general rule is: after you make a decision, do something. Don’t just go somewhere or make another decision. If you follow each decision by the action that goes with it, you can see at a glance what each decision implies.

  • Follow each decision as closely as possible with its associated action. 
  • Use data arrays to avoid repetitive control sequences.
  • Choose a data representation that makes the program simple.
  • Don’t stop with your first draft

We have presented a handful of tools for organizing the control flow and data structures of computer programs. We have also shown what can happen if not enough care is taken in these tasks. This is not to say that these are the only control constructs that lead to intelligible programs. Nor can we guarantee that using just these structures will yield a readable program. But it helps. 

The specific points of this chapter are:

 (1)  Write your program first in a made-up high-level language that you like, where you can see and debug your algorithm. When it is correct and well done, translate it into whatever language you have a compiler for. 

(2)  The control-flow constructions in your pseudo language should include: 

The ability to group statements, as in PL/I’s DD-END or BEGIN-END. 

IF-ELSE, where the ELSE part is optional. 

CASE, which is a multi-way decision. In PL/1 it can be written with a series of ELSE-IF’s. In Fortran, the computed GOTO can sometimes serve. 

Do-WHILE, which repeats a set of statements zero or more times while some condition is true. Note that the range of a Fortran DO is generally executed at least once, so the DO statement must be preceded by an IF and GOTO whenever a zero or negative repeat count might occur. 

Subroutines and functions, to break your code into small, separate, manageable pieces.

Your translation should be based on these constructions. GOTO’s and labels are suspect; use them sparingly. Any GOTO’s and labels in the final product should reflect these constructions only. 

(3)  Plan your data structures with the same care that you use for the control flow. Try to find a data representation that leads to a simpler program.  

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>