going from pseudocode to flowchart

advertisement
Underlying structures are fairly standard, even
though the particular “vocabulary” of pseudocode
may vary.
One of the first things you may want to do, when you begin analyzing pseudocode someone else has written, is to identify the various
structured-programming structures (constructs) that have been used.
This is complicated somewhat by a lack of standards for pseudocode and the existence of many different programming “vocabularies.” It is common for a programmer’s vocabulary to be influenced
by the target programming language or the programming language
the programmer is most accustomed to using. The charts below list
some of the more commonly encountered pseudocode forms along
with the programming languages that influence each form and the
standard structured-programming structure that the form implements.
So, for example, you might infer that the “Repeat … Loop until …”
form used in the Learning Activities for Modules 4, 5, 6, and 8 are
post-test “until” loops influenced by the Pascal programming language. On the other hand, you might conclude that the “Repeat
while …” form used in the Learning Activities for Module 6 and
the “Repeat until …” form used in the Learning Activities for Module 8 are not exactly like any of the listed programming languages,
but probably represent a pre-test “while” loop and a pre-test “until”
loop, respectively, similar to BASIC’s “DO WHILE …” and “DO
UNTIL …”. Since there are no hard and fast standards for pseudocode, you may have to infer another programmer’s intentions while
trying to find the standard structured-programming structure that
best fits those intentions.
Here’s a (low tech) technique that can be very helpful. Get a print
out of the pseudocode in question. Use pen or pencil (color is nice)
to draw a box around each structured-programming structure. Remember that you can have structures nested inside of other structures. So, structures that contain other structures nested inside them
may actually span several lines or more. In these cases, you will
wind up with boxes inside boxes. If the programmer of the pseudocode has been careful to indent nested structures, that can also help
you identify nested structure relationships. (Beware that the indention levels for the Learning Activities for Module 8 are not
entirely consistent.) Using boxes in this way to clarify the structures and their relationships can help you when you come to recreate the same logic using flowcharts.
Miscellaneous Statement Forms
based on …
Farrell
COBOL
BASIC
Pascal
C/C++/Java
calculation, assignment
input
output
compute, add,
= (assignment)
COMPUTE, ADD,
SUBTRACT, etc.,
MOVE, SET
= (assignment operator)
:= (assignment operator)
= (assignment operator)
get,
read
ACCEPT,
READ
print,
write
DISPLAY,
WRITE
INPUT, functions
procedures
and functions
functions and
operators
PRINT,
WRITE
procedures
functions and
operators
delimit statements in a
compound
statement
module call
perform
PERFORM,
CALL
BEGIN …
END
{…}
SFC generic pseudocode
SFC
flowchart
Farrell +
Visio
statement
statement
statement
procedure
name
Selection Structures
double and single selection
based on …
Farrell
if condition then
body
else
body
endif
if condition then
body
endif
COBOL
BASIC
IF condition
body
ELSE
body
END-IF
IF condition THEN
body
ELSEIF condition THEN
body …
ELSE
body
END IF
IF condition
body
END-IF
IF condition THEN
body
END IF
Pascal
IF condition
THEN
body
ELSE
body
IF condition
THEN
body
if (condition)
body
else
body
if (condition)
body
C/C++/Java
case construct
case based on basis
case option …
body …
default
body
endcase
EVALUATE basis
WHEN option …
body …
WHEN OTHER
body
END-EVALUATE
SELECT CASE basis
CASE option …
body …
CASE ELSE
body
END SELECT
CASE basis OF
option:
body …
OTHERWISE
body
END
switch (basis)
case option: …
body
break; …
default:
body
Selection Structures
double and single selection
based on …
SFC generic
pseudocode
IF condition THEN
body
ELSE
body
END-IF
IF condition THEN
body
END-IF
case construct
SELECT basis
CASE option:
body
END-CASE
DEFAULT-CASE:
body
END-SELECT
SFC
flowchart
Farrell +
Visio
basis
condition
condition
No
Yes
Yes
option
default
...
body
body
option
body
No
body
body
(Click here for a detailed chart of actual programming language syntax for case structures.)
body
General Loop Structures
based on …
Farrell
pre-test loop (Do-While)
post-test loop (Do-Until)
while condition
body
endwhile
COBOL
BASIC
DO WHILE condition
body
LOOP
PERFORM UNTIL condition
body
END-PERFORM
DO UNTIL condition
body
LOOP
DO
body
LOOP WHILE condition
Pascal
WHILE condition DO
body
C/C++/Java
SFC generic
pseudocode
SFC
flowchart
do
body
until condition
PERFORM WITH TEST AFTER
UNTIL condition
body
END-PERFORM
DO
body
LOOP UNTIL condition
REPEAT
body
UNTIL condition
while (condition)
body
while (true)
{
if (condition)
break;
body;
}
do
body
while (condition)
while (true)
{
body;
if (condition)
break;
}
WHILE condition DO
body
END-WHILE
LOOP
UNTIL condition
body
END-LOOP
REPEAT
body
WHILE condition
LOOP
body
UNTIL condition
END-LOOP
General Loop Structures
based on …
Farrell +
Visio
pre-test loop (Do-While)
condition
No
Yes
body
condition
No
post-test loop (Do-Until)
body
body
Yes
body
No
Yes
condition
condition
No
Yes
(Click here for a detailed chart of actual programming language syntax for general loop structures.)
Let counter = 0
Pseudo-code and flowcharts may be written at variLet total = 0
ous levels of refinement.
While counter < 3 do
Get a number
One of the techniques programmers and analysts use is that of “sucAdd number to total
cessive refinement.” When successive refinement is used, the proAdd 1 to counter
cess of understanding the problem and planning the logic may actuEndwhile
ally involve a number of smaller intermediate stages. For example,
Let average = total / counter
as you develop your procedural logic you may state a step such as
Print average
Calculate the average of three numbers.
before you are ready to start actually coding the logic of your proThis is a relatively “rough” level of refinement in that it doesn’t
gram. This level of refinement, as well, can be flowcharted as well
specify a great amount of procedural detail, but you would be peras pseudo-coded.
fectly justified in including it in the pseudocode or flowchart of
your early analysis of the problem at hand. If your programming
The Learning Activities for Modules 4, 5, 6, and 8 contain mixed
language already had a “calculate the average” function, it would be
levels of refinement. For example, the form
fairly straightforward to implement this step in actual source code.
Repeat
…
Loop until all numbers have been entered
On the other hand, if your programming language does not have
such a function, or for other possible reasons, you may need or
is a fairly unrefined level, since it does not carry much detail as to
want to take the above statement through one or more levels of suchow the computer will actually determine when all the numbers
cessive refinement (increasing levels of detail). You might ultimatehave been entered. Based on the fact that the procedural logic has
ly end up with something like this:
previously asked the user how many numbers they want to enter,
you could implement the loop as a counter-controlled loop. (But,
oops, we don’t really discuss counter-controlled loops in depth until
Module 6.) Another possible loop implementation would be to test
for end-of-file, such as we have seen in Modules 1, 2 and 4. (But,
then we wouldn’t need to ask the user ahead of the loop how many
numbers they want to enter.) Of course, it is perfectly alright to
flowchart at the same level of refinement as the given pseudocode.
In that case, we would just use “all numbers have been entered” as
the “until” condition of a post-test loop, thus finessing the issue for
now. Granted that this condition does not meet all the formal requirements of a proper Boolean expression, but it is a “yes or no”
question. So, it can stand, at least at this early level of successive
Visio
 powerful editing capabilities
 full repertoire of standard flowcharting symbols and components
 great flexibility of flowchart layout and connectivity
 easier to diagram new logic around existing logic
- twitchy dynamic connectors (arrowed lines)
- lack of built-in structure can allow a novice programmer to slip
into spaghetti logic
- careful tweaking may be required to get a good looking and
properly diagrammed flowchart
•
•
•
•
•
•
no constraints on how a flowchart is put together
Be particularly careful of how your arrowed lines lay out. An arrowhead pointing in the wrong direction can be very misleading to
the person reading your flowchart.
Make sure all your lines lead from some kind of icon, and lead to
some kind of icon or join with another line.
Make sure all lines coming out of a decision are correctly labeled.
Make sure each icon has a line coming in and one coming out—
exceptions: terminals and decisions.
Use methods similar to “Recognizing Structure” in Chapter 2 to
check to make sure your flowchart is properly structured.
The “Farrell + Visio” diagrams in the above tables are actually em-
refinement, as the decision criterion for any of the general loop
forms, or the single or double selections, for that matter.
Visio or Structured Flow Chart (SFC)?
We have made two different flowcharting programs available to
you. Which should you use? It is probably fair to say that Visio is
the more powerful and commonly available of the two. However,
each has certain advantages and disadvantages you may want to
consider. I have listed some of those in the table below, along with
a couple of tips for using each flowcharting program.
Structured Flow Chart (SFC)
- rather limited editing capabilities
- somewhat idiosyncratic symbol forms
- hard to make major revisions in module’s logic without starting
over
 automatically positions symbols and lines according to a limited
number of standard structured patterns
 constrains programmer to develop properly diagrammed structured
logic
 automatically generates two forms of pseudocode in parallel with
structured flowchart
• works best when programmer uses a top-down approach
In SFC, the pre-test (“Test at Top Loop”) and post-test (“Test at Bottom Loop”) are both “while” loops. If you need an “until” loop, use
the “User Controlled Exit Loop” option. That option provides loop
body insertion points both before and after the exit condition. In fact,
Pascal, BASIC, C, C++, and some versions of COBOL provide ways
(besides “go to’s”) to conditionally exit from the middle of a loop
body. SFC’s User Controlled Exit Loop allows for this, even though
this departs from the strict tenants of structured programming. However, if you insert your loop body steps only after the exit condition or
only before the exit condition, you will have a strictly structured pretest “until” loop or post-test “until” loop, respectively.
bedded Visio objects. So, you should be able to copy and paste them
into Visio for further editing. (Right click on a Visio diagram above.
Choose Visio Object|Edit (or Open). Right click in the edit window
and select Copy Drawing. You can then paste the drawing into an
open Visio document.)
ARR 6/30/2004
Download