Structured Programming Summary

Just as architects design buildings by employing the collective wisdom of their profession, so should programmers design programs. Our field is younger than architecture is, and our collective wisdom is considerably sparser. We have learned that structured programming produces programs that are easier than unstructured programs to understand, test, debug, modify, and even prove correct in a mathematical sense.

Figure 5.20 uses activity diagrams to summarize C++'s control statements. The initial and final states indicate the single entry point and the single exit point of each control statement. Arbitrarily connecting individual symbols in an activity diagram can lead to unstructured programs. Therefore, the programming profession uses only a limited set of control statements that can be combined in only two simple ways to build structured programs.

Figure 5.20. C++'s single-entry/single-exit sequence, selection and repetition statements.

(This item is displayed on page 218 in the print version)

For simplicity, only single-entry/single-exit control statements are usedthere is only one way to enter and only one way to exit each control statement. Connecting control statements in sequence to form structured programs is simplethe final state of one control statement is connected to the initial state of the next control statementthat is, the control statements are placed one after another in a program. We have called this "control-statement stacking." The rules for forming structured programs also allow for control statements to be nested.

Figure 5.21 shows the rules for forming structured programs. The rules assume that action states may be used to indicate any action. The rules also assume that we begin with the so-called simplest activity diagram (Fig. 5.22), consisting of only an initial state, an action state, a final state and transition arrows.

Figure 5.21. Rules for forming structured programs.

(This item is displayed on page 219 in the print version)

Rules for Forming Structured Programs

  1. Begin with the "simplest activity diagram" (Fig. 5.22).
  2. Any action state can be replaced by two action states in sequence.
  3. Any action state can be replaced by any control statement (sequence, if, if...else, switch, while, do...while or for).
  4. Rules 2 and 3 can be applied as often as you like and in any order.

Figure 5.22. Simplest activity diagram.

(This item is displayed on page 219 in the print version)

Applying the rules of Fig. 5.21 always results in an activity diagram with a neat, building-block appearance. For example, repeatedly applying Rule 2 to the simplest activity diagram results in an activity diagram containing many action states in sequence (Fig. 5.23). Rule 2 generates a stack of control statements, so let us call Rule 2 the stacking rule. [Note: The vertical dashed lines in Fig. 5.23 are not part of the UML. We use them to separate the four activity diagrams that demonstrate Rule 2 of Fig. 5.21 being applied.]

Figure 5.23. Repeatedly applying Rule 2 of Fig. 5.21 to the simplest activity diagram.

Rule 3 is called the nesting rule. Repeatedly applying Rule 3 to the simplest activity diagram results in an activity diagram with neatly nested control statements. For example, in Fig. 5.24, the action state in the simplest activity diagram is replaced with a double-selection (if...else) statement. Then Rule 3 is applied again to the action states in the double-selection statement, replacing each of these action states with a double-selection statement. The dashed action-state symbols around each of the double-selection statements represent an action state that was replaced in the preceding activity diagram. [Note: The dashed arrows and dashed action state symbols shown in Fig. 5.24 are not part of the UML. They are used here as pedagogic devices to illustrate that any action state may be replaced with a control statement.]

Figure 5.24. Applying Rule 3 of Fig. 5.21 to the simplest activity diagram several times.

(This item is displayed on page 220 in the print version)

Rule 4 generates larger, more involved and more deeply nested statements. The diagrams that emerge from applying the rules in Fig. 5.21 constitute the set of all possible activity diagrams and hence the set of all possible structured programs. The beauty of the structured approach is that we use only seven simple single-entry/single-exit control statements and assemble them in only two simple ways.

If the rules in Fig. 5.21 are followed, an activity diagram with illegal syntax (such as that in Fig. 5.25) cannot be created. If you are uncertain about whether a particular diagram is legal, apply the rules of Fig. 5.21 in reverse to reduce the diagram to the simplest activity diagram. If it is reducible to the simplest activity diagram, the original diagram is structured; otherwise, it is not.

Figure 5.25. Activity diagram with illegal syntax.

(This item is displayed on page 222 in the print version)

Structured programming promotes simplicity. Böhm and Jacopini have given us the result that only three forms of control are needed:

  • Sequence
  • Selection
  • Repetition

The sequence structure is trivial. Simply list the statements to execute in the order in which they should execute.

Selection is implemented in one of three ways:

  • if statement (single selection)
  • if...else statement (double selection)
  • switch statement (multiple selection)

It is straightforward to prove that the simple if statement is sufficient to provide any form of selectioneverything that can be done with the if...else statement and the switch statement can be implemented (although perhaps not as clearly and efficiently) by combining if statements.

Repetition is implemented in one of three ways:

  • while statement
  • do...while statement
  • for statement

It is straightforward to prove that the while statement is sufficient to provide any form of repetition. Everything that can be done with the do...while statement and the for statement can be done (although perhaps not as smoothly) with the while statement.

Combining these results illustrates that any form of control ever needed in a C++ program can be expressed in terms of the following:

  • sequence
  • if statement (selection)
  • while statement (repetition)

and that these control statements can be combined in only two waysstacking and nesting. Indeed, structured programming promotes simplicity.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Similar book on Amazon © 2008-2017.
If you may any questions please contact us: