Вы находитесь на странице: 1из 7

Pseudocode is a kind of structured english for describing algorithms.

It allows the designer to focus


on the logic of the algorithm without being distracted by details of language syntax. It describe the
entire logic of the algorithm so that implementation becomes a rote mechanical task of translating
line by line into source code.
In general the vocabulary used in the pseudocode should be the vocabulary of the problem domain,
not of the implementation domain. The pseudocode is a narrative for someone who knows the
requirements (problem domain and is trying to learn how the solution is organi!ed. ".g.,
"xtract the next word from the line (good
set word to get next token (poor
#ppend the file extension to the name (good
name $ name % extension (poor
&'( all the characters in the name (good
&'( character $ first to last (ok
)o standard for pseudocode syntax exists, as a program in pseudocode is not an executable
program.
(*+", &'( P,"*-'.'-"
/. 0rite only one stmt per line
"ach stmt in your pseudocode should express 1ust one action for the com
puter.
If the task list is properly drawn, then in most cases each task will correspond to
one line of pseudocode.
"23
T#,4 +I,T3
(ead name, hourly rate, hours worked, deduction rate
Perform calculations
gross $ hourly(ate 5 hours0orked
deduction $ g
rossPay 5 deduction(ate
net pay $ grossPay
6
deduction
0rite name, gross, deduction, net pay
P,"*-'.'-"3
("#- name, hourly(ate, hours0orked, deduction(ate
grossPay $ hourly(ate 5 hours0orked
deduction $ grossPay 5 deduction(ate
netPay $ grossPay
6
d
eduction
0(IT" name, grossPay, deduction, netPay
7..apitali!e initial keyword
In the example above,
("#-
and
0(IT"
are in caps. There are 1ust a few
keywords we will use3
("#-, 0(IT", I&, "+,", ")-I&, 08I+", ")-08I+", ("P"#T, *)TI+
9.Indent to show hier
archy
0e will use a particular indentation pattern in each of the design structures3
,":*")."
3 keep statements that are ;stacked< in sequence all starting in the
same column.
,"+".TI')
3 indent the statements that fall inside the selection structure, bu
t not
the keywords that form the selection
+''PI)=
3
indent the statements that fall inside the loop, but not the keywords
that form the loop
"23 In the example above, employees whose
grossPay
is less than />> do not
have any deduction.
T#,4 +I,T3
(ead
name, hourly rate, hours worked, deduction rate
.ompute gross, deduction, net pay
Is gross ?$ />>@
A",3 calculate deduction
)'3 no deduction
0rite name, gross, deduction, net pay
P,"*-'.'-"3
("#- name, hourly(ate, hours0orked
grossPay $ h
ourly(ate 5 hours0orked
I& grossPay ?$ />>
deduction $ grossPay 5 deduction(ate
"+,"
deduction $ >
")-I&
netPay $ grossPay
6
deduction
0(IT" name, grossPay, deduction, netPay
B."nd multiline structures
,ee how the I&C"+,"C")-I& is constructed abo
ve. The ")-I& (or ")-
whatever always is in line with the I& (or whatever starts the structure.
D.4eep stmts language independent
,"+".TI') ,T(*.T*("
0e looked at this in .hap 73
The
pseudocode
for this would be3
I& amount E />>>
interest(ate $ .>F
CC the ;yes< or ;true< action
"+,"
interest(ate $ ./>
CC the ;no< or ;false< action
")-I&
,ome selections are of the ;do it
or donGt< (one sided variety. &or example3
The pseudocode for this is3
I& average E F>
-' ,end0arning
")-I&
amount
E />>>
interest(ate $
.>F
interest(ate $
./>
yes
no
average
E F> @
-'
,end0arning
yes
no
It is considered
poor form
to have a /
H
sided I& stmt where the action is on the ;no< or
"+," side. .onsider this code3
I& average
E F>
)*++
"+,"
-' =ivePassing=rade
")-I&
This could (
and should
be rewritten to eliminate the )*++ ;yes< part. To do that, we
change the E to its opposite3 ?$ as follows3
I& average ?$ F>
-' =ivePassing=rade
")-I&
)",TI)= I& ,T#T"I")T,
0hat if we wanted to put a little menu up on the screen3
/.,olitaire
7.-oom
9.Ionopoly
and have the user select which game to play. 8ow would we activate the correct
game@
("#- game)umber
I& game)umber $ /
-' ,olitaire
"+,"
I& game)umber $ 7
-' -o
om
"+,"
-' Ionopoly
")-I&
")-I&
0e must pay particular attention to where the I&s end. The nested I& must be
completely
contained in either the I& or the "+," part of the containing I&. 0atch for
and line up the matc
hing ")-I&.
("#-
game)umber
game)umber
$ / @
game)umber
$ 7 @
no
-'
,olitaire
-' -oom
-'
Io
nopoly
no
yes
yes
+''PI)= ,T(*.T*(",
'ne of the most confusing things for students first seeing a flowchart is telling the loops
apart from the selections. This is because both use the diamond shape as their control
symbol. In pseudocode this confusion is eli
minated. To mark our loops we will use
these pairs3
08I+"
C
")-08I+"
("P"#T
C
*)TI+
The loop shown here (from the last
chapter will have the following
pseudocode3
count $ >
08I+" count E />
#-- / to count
0(IT" count
")-08I+"
0(IT"
;The end<
)otice that the connector and test at the
top of the loop in the flowchart become
the 08I+" stmt in pseudocode. The
end of the loop is marked by
")-08I+".
0hat statement do we execute when
the loop is over@ The one that follows
the ")-08
I+".
,ometimes it is desirable to put the steps that are inside the loop into a separate
module. Then the pseudocode might be this3
Iainline
0e often use this name for the first module.
count $ >
Initiali!ation comes first
08I+" count E />
-' Proce
ss
The processing loop uses this module
")-08I+"
0(IT" ;The end<
Termination does clean
H
up
Process
=o thru these steps and then return to the
#-- / to count
module that sent you here (Iainline
0(IT" count
,T#(T
.ount $ >
.ount
E />
#dd / to count
,T'P
0rite
count
no
yes
0rite
;The
end<
This time we will see how to
write pseudocode
for an *)TI+ loop3
count $ >
("P"#T
#-- / to count
0(IT" count
*)TI+ count ?$ />
0(IT" ;The end<
)otice how the connector at the top of the loop
corresponds to the ("P"#T keyword, while
the test at the bottom corresponds the the
*)TI
+ stmt. 0hen the loop is over, control
goes to the stmt following the *)TI+.
,T#(T
.ount $ >
#dd / to count
0rite
count
.ount
?$ />
,T'P
no
yes
0rite
;The end<
#-J#)T#=", #)- -I,#-J#)T#=",
Pseudocode -isadvantages

ItGs not visual

There is no accepted standard, so it varies widely from company to company


Pseudocode #dvantages

.an be d
one easily on a word processor

"asily modified

Implements structured concepts well


&lowchart -isadvantages

8ard to modify

)eed special software (not so much nowK

,tructured design elements not all implemented


&lowchart #dvantages

,tandardi!ed3 all pret


ty much agree on the symbols and their meaning

Jisual (but this does bring some limitations

Вам также может понравиться