Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - JavaScript: Control Statements II

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Summary of Structured Programming

Just as architects design buildings by employing the collective wisdom of their profession, so should programmers design programs.

Summary of Structured Programming

 

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

 

Flowcharts reveal the structured nature of programs or the lack thereof. Connecting individual flowchart symbols arbitrarily can lead to unstructured programs. Therefore, the programming profession has chosen to combine flowchart symbols to form a limited set of control structures and to build structured programs by properly combining control structures in two simple ways.

For simplicity, only single-entry/single-exit control structures are used—that is, there is only one way to enter and one way to exit each control structure. Connecting control structures in sequence to form structured programs is simple: The exit point of one control structure is connected to the entry point of the next control structure (i.e., the control structures are simply placed one after another in a program). We have called this process control-structure stacking. The rules for forming structured programs also allow for con-trol structures to be nested. Figure 8.20 summarizes JavaScript’s control structures. Small circles are used in the figure to indicate the single entry point and the single exit point of each structure.

 

Figure 8.21 shows the rules for forming properly structured programs. The rules assume that the rectangle flowchart symbol may be used to indicate any action, including input/output. [Note: An oval flowchart symbol indicates the beginning and end of a pro-cess.]

 

Applying the rules in Fig. 8.21 always results in a structured flowchart with a neat, building-block-like appearance. For example, repeatedly applying Rule 2 to the simplest




flowchart (Fig. 8.22) results in a structured flowchart containing many rectangles in sequence (Fig. 8.23). Note that Rule 2 generates a stack of control structures; so let us call Rule 2 the stacking rule.

 

Rule 3 is called the nesting rule. Repeatedly applying Rule 3 to the simplest flowchart results in a flowchart with neatly nested control structures. For example, in Fig. 8.24, the rectangle in the simplest flowchart is first replaced with a double-selection (ifelse) structure. Then Rule 3 is applied again to both of the rectangles in the double-selection structure by replacing each of these rectangles with double-selection structures. The



dashed box around each of the double-selection structures represents the rectangle in the original simplest flowchart that was replaced.

 

Rule 4 generates larger, more involved and more deeply nested structures. The flow-charts that emerge from applying the rules in Fig. 8.21 constitute the set of all possible structured flowcharts and thus the set of all possible structured programs.

 

The beauty of the structured approach is that we use only seven simple single-entry/ single-exit pieces and assemble them in only two simple ways. Figure 8.25 shows the kinds of stacked building blocks that emerge from applying Rule 2 and the kinds of nested building blocks that emerge from applying Rule 3. The figure also shows the kind of over-lapped building blocks that cannot appear in structured flowcharts (because of the elimi-nation of the goto statement).

 

If the rules in Fig. 8.21 are followed, an unstructured flowchart (like the one in Fig. 8.26) cannot be created. If you are uncertain about whether a particular flowchart is structured, apply the rules of Fig. 8.21 in reverse to try to reduce the flowchart to the sim-


plest flowchart. If the flowchart is reducible to the simplest flowchart, the original flow-chart is structured; otherwise, it is not.

 

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

 

sequence

 

selection

 

repetition

 

Sequence is trivial. Selection is implemented in one of three ways:

 

if statement (single selection)

 

ifelse statement (double selection)

 

switch statement (multiple selection)

 

In fact, it is straightforward to prove that the if statement is sufficient to provide any form of selection; everything that can be done with the ifelse statement and the switch statement can be implemented by combining if statements (although perhaps not as smoothly).

Repetition is implemented in one of four ways:

 

while statement

 

dowhile statement

 

for statement

 

forin statement (discussed in Chapter 10)

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

 

Combining these results illustrates that any form of control ever needed in a Java-Script program can be expressed in terms of:

 

sequence

 

if statement (selection)

 

while statement (repetition)

 

These control structures can be combined in only two ways—stacking and nesting. In-deed, structured programming promotes simplicity.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.