Wednesday, January 15, 2014

Normally, statements in a program execute one after the other in the order in which they
appear in the program. This is called sequential execution. Various C# statements enable
the programmer to specify that the next statement to execute may not be the next one in
sequence. A transfer of control occurs when a statement other than the next one in the pro-
gram executes.

During the 1960s, it became clear that the indiscriminate use of transfers of control was
causing difficulty for software development groups. The problem was the goto statement,
which, in some programming languages, allows the programmer to specify a transfer of
control to one of a wide range of possible destinations in a program. This caused programs
to become quite unstructured and hard to follow. The notion of structured programming
became almost synonymous with “goto elimination.”

The research of Bohm and Jacopini1 demonstrated that all programs with goto state-
ments could be written without them. The challenge of the era for programmers was to shift
their  styles  to  “goto-less  programming.”  It  was  not  until  the  1970s  that  programmers
started taking structured programming seriously. The results were impressive, as software
development groups reported reduced development times, more frequent on-time delivery
of systems and more frequent within-budget completion of software projects. The key to
these successes was that structured programs were clearer, easier to debug and modify and
more likely to be bug-free in the first place.

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of
only three control structures, namely, the sequence structure, the selection structure and
the repetition structure. The sequence structure is built into C#. Unless directed otherwise,
the computer executes C# statements one after the other in the order in which they appear
in a program. The flowchart segment of Fig. 4.1 illustrates a typical sequence structure in
which two calculations are performed in order.

A flowchart is a graphical representation of an algorithm or of a portion of an algo-
rithm. Flowcharts contain certain special-purpose symbols, such as rectangles, diamonds,
ovals and small circles. These symbols are connected by arrows called flowlines, which
indicate the order in which the actions of the algorithm execute. This order is known as the
flow of control.

Like pseudocode, flowcharts often are useful for developing and representing algo-
rithms, although pseudocode is preferred by many programmers. Flowcharts show clearly
how control structures operate; that is all we use them for in this text. The reader should
compare carefully the pseudocode and flowchart representations of each control structure.


1.  Bohm, C., and G. Jacopini, “Flow Diagrams, Turing Machines, and Languages with Only Two
Formation Rules,” Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336–371.

Consider the flowchart segment for the sequence structure in Fig. 4.1. We use the rect-
angle symbol, also called the action symbol, to indicate any type of action, including a cal-
culation or an input/output operation. The flowlines in the figure indicate the order in which
the actions are to be performed—first, studentGrade is to be added to total, then 1
is to be added to counter. We can have as many actions as we want in a sequence struc-
ture. Anywhere in a sequence that a single action may be placed, several actions may also
be placed.

When drawing a flowchart that represents a complete algorithm, an oval symbol con-
taining the word “Begin” is the first symbol used; an oval symbol containing the word
“End” indicates where the algorithm ends. When drawing only a portion of an algorithm,
as in Fig. 4.1, the oval symbols are omitted in favor of using small circle symbols, also
called connector symbols.

Perhaps the most important flowcharting symbol is the diamond symbol, also called
the decision symbol, which indicates that a decision is to be made. We discuss the diamond
symbol in Section 4.5.

C# provides three types of selection structures, which we discuss in this chapter and
the next. The if selection structure performs (selects) an action if a condition is true or
skips  the action  if  the  condition is false. The  if/else  selection  structure  performs  an
action if a condition is true and performs a different action if the condition is false. The
switch selection structure, discussed in Chapter 5, Control Structures: Part 2, performs
one of many actions, depending on the value of an expression.

The if structure is called a single-selection structure because it selects or ignores a
single action (or a single group of actions). The if/else structure is called a double-selec-
tion structure because it selects between two different actions (or groups of actions). The
switch structure is called a multiple-selection structure because it selects among many
different actions (or groups of actions).

C#  provides  four  repetition  structures—while,  do/while,  for  and  foreach
(while is covered in this chapter, do/while and for are covered in Chapter 5, Control
Structures: Part 2, and foreach is covered in Chapter 8, Object-Based Programming).
Each of the words if, else, switch, while, do, for and foreach are C# keywords.
Figure 4.2 lists the complete set of C# keywords. We discuss the vast majority of C#’s key-
words throughout this book.

C# has only eight control structures—sequence, three types of selection and four types
of repetition. Each program is formed by combining as many of each type of control struc-
ture as is necessary. As with the sequence structure in Fig. 4.1, each control structure is
flowcharted with two small circle symbols, one at the entry point to the control structure
and one at the exit point.

Single-entry/single-exit control structures make it easy to build programs—the control
structures are attached to one another by connecting the exit point of one control structure
to the entry point of the next. This is similar to the stacking of building blocks; thus, we call
it control-structure stacking. There is only one other way control structures may be con-
nected, and that is through control-structure nesting, where one control structure can be
placed inside another. Thus, algorithms in C# programs are constructed from only eight dif-
ferent types of control structures combined in only two ways.

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!