THREE BASIC STRUCTURES
In the mid-1960s, mathematicians proved that
any program, no matter how complicated, can
be constructed using one or more of only
three structures. A structure is a basic unit of
programming logic; each structure is one of
the following:
sequence
selection
loop
SEQUENCE STRUCTURE
Performs actions or tasks in order, one after the
other. A sequence can contain any number of
tasks, but there is no option to branch off and
skip any of the tasks. Once you start a series of
actions in a sequence, you must continue step
by step until the sequence ends
SELECTION/DECISION STRUCTURE
With this structure, one of two courses of
action is taken based on the answer to a
question. A flowchart that describes a selection
structure begins with a decision symbol, and
the branches of the decision must join at the
bottom of the structure
These if-else examples can also be called dual-alternative ifs
(or dual-alternative selections) because they contain two
alternatives—the action taken when the tested condition is
true and the action taken when it is false.
Single-alternative selection structure
Note that it is perfectly correct for one branch
of the selection to be a “do nothing” branch.
The branch in which no action is taken is called
the null case or null branch
LOOP STRUCTURE
A loop continues to repeat actions while a
condition remains true. The action or actions
that occur within the loop are the loop body. In
the most common type of loop, a condition is
evaluated; if the answer is true, you execute
the loop body and evaluate the condition again.
If the condition is still true, you execute the
loop body again and then reevaluate the
condition. This continues until the condition
becomes false, and then you exit the loop
structure.
You may hear programmers refer to looping as
repetition or iteration.
Combining Structures
All logic problems can be solved
using only these three structures
—sequence, selection, and loop.
The structures can be combined
in an infinite number of ways.
For example, you can have a
sequence of tasks followed by a
selection, or a loop followed by a
sequence. Attaching structures
end to end is called stacking
structures.
Nesting Structures
- any sequence, selection, or loop can
contain other sequence, selection, or
loop structures. For example, you can
have a sequence of three tasks on one
branch of a selection, as shown in the
figure. Placing a structure within
another structure is called nesting
structures
Flowchart and pseudocode
showing nested structures—a loop
nested within a sequence, nested
within a selection
There is no limit to the number of
levels you can create when you nest
and stack structures. For example, a
Figure shows logic that has been made
more complicated by replacing stepN
with a selection.
The process of buying and planting flowers in the spring
QUICK REFERENCE-The Three Structures
In summary, a structured program has the following characteristics:
A structured program includes only combinations of the
three basic structures— sequence, selection, and loop.
Any structured program might contain one, two, or all
three types of structures.
Each of the structures has a single entry point and a
single exit point
Structures can be stacked or connected to one another
only at their entry or exit points
Any structure can be nested within another
structure