Handout01 PDF
Handout01 PDF
Algorithms
Any computing problem can be solved by executing a series of actions in a specific order.
A procedure for solving a problem in terms of
get dressed, (5) eat breakfast, (6) carpool to work. This routine gets the executive to work
well-prepared to make critical decisions.
Suppose that the same steps are performed in a slightly different order: (1) get out of
bed, (2) take off pajamas, (3) get dressed, (4) take a shower, (5) eat breakfast, (6) carpool
to work. In this case, our executive shows up for work soaking wet.
The importance of correctly specifying the order in which actions appear applies to
computer programs, as well. Program control refers to the task of ordering a program’s
statements correctly. In this chapter, we begin to investigate the program control capabili-
ties of C#.
4.3 Pseudocode
Pseudocode is an artificial and informal language that helps programmers develop algo-
rithms. The pseudocode we present is particularly useful for developing algorithms that
will be converted to structured portions of C# programs. Pseudocode is similar to everyday
English; it is convenient and user-friendly, and it is not an actual computer programming
language.
Pseudocode is not executed on computers. Rather, pseudocode helps the programmer
“think out” a program before attempting to write it in a programming language, such as C#.
In this chapter, we provide several examples of pseudocode algorithms.
Software Engineering Observation 4.1
Pseudocode helps the programmer conceptualize a program during the program design pro-
cess. The pseudocode may then be converted to C#. 4.1
The style of pseudocode that we present consists solely of characters, thus program-
mers may type pseudocode conveniently using an editor program. Programmers can con-
vert carefully prepared pseudocode programs to corresponding C# programs easily. In
many cases, this conversion takes place simply by replacing pseudocode statements with
their C# equivalents.
Pseudocode normally describes only executable statements—the actions that are per-
formed when the pseudocode is converted to C# and executed. Declarations are not execut-
able statements. For example, the declaration
int i;
informs the compiler of the type of variable i and instructs the compiler to reserve space
in memory for this variable. This declaration does not cause any action, such as input, out-
put or a calculation, to occur when the program executes. Some programmers choose to list
variables and their purposes at the beginning of a pseudocode program.
csphtp1_05.fm Page 166 Tuesday, November 13, 2001 3:50 PM
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 to understand, test, debug, modify and prove correct in a
mathematical sense than unstructured programs.
Figure 5.22 summarizes C#’s control structures. Small circles in the figure indicate the
single entry point and the single exit point of each structure. Connecting individual flowchart
symbols arbitrarily can lead to unstructured programs. Therefore, the programming profes-
sion has chosen to combine flowchart symbols to form only a limited set of control structures
and to build structured programs by combining control structures in only two simple ways.
For simplicity, only single-entry/single-exit control structures are used—there is only
one way to enter and only one way to exit each control structure. To connect control struc-
tures in sequence to form structured programs, the exit point of one control structure is con-
nected to the entry point of the next control structure (i.e., the control structures are simply
placed one after another in a program). We call this process “control-structure stacking.”
The rules for forming structured programs also allow control structures to be nested.
Figure 5.23 contains the rules for forming properly structured programs. The rules assume
that the rectangle flowchart symbol can indicate any action, including input/output.
structures.
F F
do/while structure
. switch structure
.
.
csphtp1_05.fm Page 167 Tuesday, November 13, 2001 3:50 PM
(multiple selection)
T break
F T
T break F
F
.
. for structure/foreach structure
.
T
break
F
T
F
break
Applying the rules of Fig. 5.23 always results in a structured flowchart with a neat,
building-block appearance. For example, repeatedly applying rule 2 to the simplest flow-
chart results in a structured flowchart that contains many rectangles in sequence (Fig. 5.25).
Notice that rule 2 generates a stack of control structures; therefore, we call rule 2 the
stacking rule.
Rule 3 is the nesting rule. Repeatedly applying rule 3 to the simplest flowchart results in
a flowchart with neatly nested control structures. For example, in Fig. 5.26, the rectangle in
the simplest flowchart first is replaced with a double-selection (if/else) structure. Then
rule 3 is applied again to both rectangles in the double-selection structure, replacing each of
the rectangles with a double-selection structure. The dashed boxes around each of the double-
selection structures represent the rectangles that were replaced with these structures.
Good Programming Practice 5.11
Too many levels of nesting can make a program difficult to understand. As a general rule, try
to avoid using more than three levels of nesting. 5.11
Rule 4 generates larger, more involved and deeply-nested structures. The flowcharts
that emerge from applying the rules in Fig. 5.23 constitute the set of all possible structured
flowcharts and the set of all possible structured programs.The structured approach has the
advantage of using only eight simple single-entry/single-exit pieces and allowing us to
assemble them in only two simple ways. Figure 5.27 shows the kinds of correctly stacked
building blocks that emerge from applying rule 2 and the kinds of correctly nested building
blocks that emerge from applying rule 3. The figure also shows the kind of overlapped
building blocks that cannot occur in structured flowcharts (as a result of avoiding goto
statements).
csphtp1_05.fm Page 169 Tuesday, November 13, 2001 3:50 PM
.
.
.
Fig. 5.25 Repeatedly applying rule 2 of Fig. 5.23 to the simplest flowchart.
Rule 3
Rule 3 Rule 3
If the rules in Fig. 5.23 are followed, an unstructured flowchart (such as that in
Fig. 5.28) cannot be created. If you are uncertain about whether a particular flowchart is
structured, apply the rules in Fig. 5.23 in reverse to try to reduce the flowchart to the sim-
plest flowchart. If the flowchart can be reduced to the simplest flowchart, the original flow-
chart is structured; otherwise, it is not.
In summary, structured programming promotes simplicity. Bohm and Jacopini have
found that only three forms of control are necessary:
• Sequence
• Selection
• Repetition
Sequence is trivial. Selection is implemented in one of three ways:
• if structure (single selection)
• if/else structure (double selection)
• switch structure (multiple selection)
In fact, it is straightforward to prove that the if structure is sufficient to provide any form
of selection. Everything that can be done with the if/else and switch structures can be
implemented by combining if structures (although perhaps not as elegantly).
Repetition is implemented in one of four ways:
• while structure
• do/while structure
• for structure
• foreach structure (discussed in Chapter 7)
It is straightforward to prove that the while structure is sufficient to provide any form of
repetition. Everything that can be done with the do/while, for and foreach structures
can be done with the while structure (although perhaps not as elegantly).
Combining these results illustrates that any form of control ever needed in a C# pro-
gram can be expressed in terms of:
• sequence
• if structure (selection)
• while structure (repetition)
These control structures can be combined in only two ways—stacking and nesting. Indeed,
structured programming promotes simplicity.