-
Chapter 3 covers understanding structure
-
Unstructured programming is often
-
referred to as spaghetti code.
-
Now spaghetti code might be code that can
-
be written into a working program but
-
it's often difficult to read and maintain
-
And this Spaghetti code or ustructured
-
programs are considered such because they
-
don't follow a few basic rules regarding
-
structured logic.
-
First let's talk about the three basic
-
structures. The sequence structure which
-
is performing actions in order, there's no
-
branching or skipping. It's perform one
-
task then perform the next task and then
-
the next and so on to any number of tasks
-
in a sequence. The second structure is
-
the selection or decision. We say ask a
-
question and then take an action based
-
on the answer to the question.
-
The question is going to be in the form
-
of some sort of boolean expression. Does
-
the answer to the question result in a
-
TRUE or FALSE, yes or no.
-
We are going to talk about dual
-
alternative and single alternative IF
-
selections. The third structure is called
-
the loop. A loop repeats an action or a
-
series of actions again based on the
-
answer to a question.
-
Here is the basic flow chart symbol of a
-
sequence structure. We're going to talk
-
about entry points and exit points that
-
structures have one entry here into
-
the structure and one exit here.
-
A selection structure, again one entry
-
point, one exit point. The question is
-
asked, represented by the decision symbol,
-
and depending on if the expression
-
evaluates to TRUE the flow of the program
-
takes one route if the expression
-
evaluates to FALSE the program logic
-
takes another route but notice regardless
-
of which route there is one exit point
-
for the structure.
-
Here's a pseudo code example of a dual
-
alternative IF again what we saw in the
-
previous slide here dual alternative IF
-
depending on the question you take either
-
this route or the dual route, the
-
alternate route. True versus false.
-
Dual alternative IF contains two
-
alternatives. If some condition is TRUE
-
then do one thing, else do the other.
-
So dual alternative, you have this
-
alternative or this alternative.
-
Another example of a decision structure
-
is the single alternative IF, in other
-
words if the condition evaluates to TRUE
-
we do some action otherwise we do nothing.
-
In this example if employee belongs to
-
dentalPlan then deduct $40 from
-
employeeGrossPay. There is no else clause
-
because there is not alternative, there
-
isn't that second alternative. If it's
-
TRUE deduct $40, if not exit the structure.
-
Here's the flow chart diagram of a single
-
alternative if. Again the question is
-
asked in the diamond, if the question or
-
expression evaluates to TRUE we follow
-
this route. If the expression is not TRUE
-
we follow this route. Again notice one
-
single entry, one single exit point.
-
The loop structure, again loops repeat a
-
set of actions based on the answer to that
-
boolean expression or to that boolean
-
question. The actions that are repeated
-
are referred to as the loop body. Again
-
a loop is sometimes called a repetition or
-
an iteration. Most of the time it's most
-
common to ask the question first, then
-
depending on the answer go into the loop
-
but there is such a thing called a
-
post-test loop. A pre-test loop we ask
-
the question first, if the evaluates to
-
TRUE, let's do what the body of the loop
-
says. The post-test loop would do the
-
body of the loop then ask the question
-
and depending on the answer decide whether
-
or not to repeat or reiterate the body of
-
the loop. And here is the flow chart
-
diagram of this.
-
The entry point is here the exit
-
point is here.
-
And then we come into here and we ask the
-
question. If the data evaluates to TRUE
-
based on the question then we do the
-
body of the loop. We come back here and
-
determine whether or not we go into the
-
loop again. Now we are going to learn
-
that the data that is being evaluated
-
that data that was originally picked up
-
here, we call it the sentinel value we
-
have to change that sentinel value
-
somewhere in the body of the loop. We
-
did talk about this in class so I'll
-
apologize for the confusion that I may
-
have caused here. But again notice that
-
there's one entry point, one exit point.
-
Eventually when you get to the point
-
where the expression evaluates to FALSE
-
you exit the structure.
-
Here's some pseudo code of a loop
-
structure. Again just generically while
-
the test condition continues to be TRUE
-
do something and an example.
-
All logic problems can be solved using
-
these three structures. These three
-
structures can be combined infinitely
-
in an infinite number of ways. Stacking
-
the structures, in other words having a
-
sequence immediately followed by a loop
-
immediately followed by a decision or any
-
variation from there. The decision
-
structures and the loop structures require
-
some sort of end-structure statement to
-
tell us we are done with this structure.
-
Typically that is an endif statement for
-
the decision and an end while statement
-
for the loop. Now again in pseudo code it
-
doesn't have to be this exact word endif
-
but it will be something like that or
-
this here. When you actually write the
-
program code these words would be
-
language specific. Here's an example of
-
stacking the three structures. You have
-
a sequence here. Notice that the sequence
-
one entry point one exit point. The exit
-
point of the sequence is the entry point
-
to the selection, which is here. Again
-
the selection has the one entry point the
-
one exit point. The selection's exit point
-
also serves as the entry point for the
-
loop which is here. So that is stacking.
-
Notice in the pseudo code we have step A
-
step B, that's your sequence. Immediately
-
followed by the decision if end if.
-
Notice the indentation. And then the loop
-
the while and the end while to end
-
the loop.
-
Not only can you stack the three
-
structures you can also nest them.
-
Nesting is placing one structure inside
-
another structure. In the pseudo code
-
again we would indent the nested structure
-
statement. This term block, a block of
-
code or a block of logic is a group of
-
statements that execute as a single unit.
-
And again that is very typical of what you
-
might find in the body of the loop.
-
Quite often you are going to find more
-
than one statement or action within the
-
body of a loop. But that can pertain to
-
decisions and structures as well.
-
Now here's an example of a sequence, steps
-
J, K, and L is a sequence which is nested
-
inside a decision. Notice the pseudo code
-
the decision begins with the if statement
-
and ends with the endif. The sequence is
-
indented inside the decision. Here's an
-
example of a loop this is a loop which is
-
nested inside the sequence, here's the
-
seqence, and the sequence is nested inside
-
a decision. Again, notice the pseudo code
-
notice the indentation.
-
Again these types of combinations can be
-
infinite. Another example.
-
Structured programs have the following
-
characteristics, they include only
-
combinations of the three basic structures
-
The sequence, the decision, and the loop.
-
Each of the structures has a single entry
-
and exit point.
-
Structures can be stacked or connected to
-
one another only at their entry or exit
-
points. And remember as I showed
-
the exit point of one is typically the
-
entry point for the next structure. And
-
any structure can be nested
-
within another structure.