0% found this document useful (0 votes)
137 views9 pages

1 04 Pseudocode Style Guide

The document provides guidelines for writing pseudocode, including style recommendations. It discusses using pseudocode during the software design process to model algorithms before implementation in a programming language. Pseudocode aims to be language-independent and easily modifiable to allow for rapid design, evaluation and modification of algorithms. The document provides examples of pseudocode for simple operations, alternation, repetition and modules. It recommends a structured English approach using action verbs and meaningful identifiers.

Uploaded by

bims
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views9 pages

1 04 Pseudocode Style Guide

The document provides guidelines for writing pseudocode, including style recommendations. It discusses using pseudocode during the software design process to model algorithms before implementation in a programming language. Pseudocode aims to be language-independent and easily modifiable to allow for rapid design, evaluation and modification of algorithms. The document provides examples of pseudocode for simple operations, alternation, repetition and modules. It recommends a structured English approach using action verbs and meaningful identifiers.

Uploaded by

bims
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PSEUDOCODE STYLE GUIDE

INTRODUCTION
During the design phase, software developers create models of different aspects of the
system. Each type of model provides a different viewpoint on the prospective system.
Some emphasize functionality, some highlight data, and others model behavior of the
system in response to its environment.
As the design process progresses, the software developer refines high-level, abstract
models into more concrete models by adding detail. This process reflects increased
understanding of the program components. The culmination of the design process is the
creation of the most detailed model of the system: the pseudocode.
Pseudocode captures the final decomposition of the problem into its most refined design.
It embodies all three kinds of modeling perspectives: functionality, data, and state
transition. The purpose of the pseudocode is to represent the algorithmic level of program
functionality, including how the functions utilize the data and how the program reacts to
events and changes state. Pseudocode is essential during program design in allowing the
programmer to work through complex details of the algorithm without being concerned
with minor aspects of language implementation. (The final implementation phase is the
proper arena for addressing language-specific issues such as input/output formatting and
exact statement syntax.)
Pseudocode has the following properties:

• language independence which allows the implementation to occur in any


programming language and eliminates concern about small language-specific
details that are irrelevant to the algorithm itself

• ease of modification which allows the developer to rapidly produce, evaluate,


modify and even discard algorithms
The pseudocode must be consistent with previous models of the system including the
structure chart, data dictionary, data flow diagrams, and state transition diagrams. For
example, data elements identified in the pseudocode should be present and defined within
the data dictionary. All modules outlined in the structure chart should have a
corresponding pseudocode module. Data flow indicated on the structure chart into and
out of modules should match the pseudocode module interface description.
During the implementation phase, the pseudocode is translated into a programming
language. Pseudocode statements are often used as program commentary and are
especially appropriate as block comments describing the purpose and actions within
sections of code.
Just as with code, good pseudocode should follow style guidelines. Style guidelines
ensure readability and consistency which are essential in team production of software.
Consistent style can also facilitate early error detection and ease the translation process
during implementation.
Pseudocode is also used within the computer science community to communicate
algorithms in various scholarly works such as texts, periodicals, conference proceedings,
and seminar notes.

STYLE OVERVIEW
Good pseudocode style includes:

• consistent use of meaningful identifiers

• proper indentation of statements

• use of appropriate control structures

• use of good data coupling

• use of good modular cohesion

• use of meaningful internal documentation


The following sections will illustrate the use of pseudocode to represent:

• simple sequence operations

• alternation operations

• repetition operations

• modules definitions and activations


Pseudocode makes use of "Structured English". The rules for Structured English are quite
simple:
Every statement appears on one or more lines by itself
Every statement begins with an action word or verb
Following the verb is one or more nouns, representing data objects that are operated
on by the verb.
Adverbs and adjectives are not used.
Conjunctions and prepositions are held to a minimum.
Following is a partial list of action words or verbs that may be used in pseudocode
statements:
Read Write Prompt Open Close Display
Add Subtract Multiply Divide Calculate Insert
Delete Remove Append Find Access Index
Call Activate Do Perform Set
Nouns used to name data objects should consist of whole words or standard
abbreviations. Some examples include:
Entry Operand Response Value Rate Price
Volume Salary Number Name Address Quantity
Total Subtotal idNumber convRate unitPrice priceRange
cityState Zip telPrefix payRate cm2Inch upCase

Once a noun is used for a specific data object, that noun must be used exclusively.
Changing the data object's name or spelling makes your design read like a Russian novel.
Constant data should be written using all capital letters:
BONUS DISCOUNT MAX_SCORE RATE
THRESHOLD
The following logical, relational, and arithmetic operations can be used within
pseudocode expressions:
+ - * / div mod
and or not
= <> > < >= <=
Assignment of a value to a data element is done as follows:
Set x to 3
Every multi-line statement that contains a program control structure will begin and end
with a keyword. Following is a list of program control structure keywords:
Control Structure Ending Keyword
If Endif
Case Endcase
While Endwhile
Repeat Until
For Endfor
SIMPLE SEQUENCE OPERATIONS
Simple sequence operations may be broken into two primary groups 1) those that perform
input/output operations and 2) those that perform data manipulations operations. Some
sample pseudocode statements for simple sequence operations include:
Input/Output Operations
Prompt for menu selection
Display total with labels
Read selection
Write totals with labels to printer
Open inputFile using fileName
Write name, idCode, address to outFile
Close inputFile
Data Manipulation Operations
Compute grossPay
Add value to total
Compute average by dividing sum by count
Remove record
Subtract expense from subTotal
Append record
Calculate circumference
ALTERNATION OPERATIONS
The three types of alternation commonly used in procedural programming are:

• One-tailed alternation / selection ( if / then )

• Two-tailed alternation / selection ( if / then / else )

• Multi-tailed alternation / selection / ( case )


Here are some sample pseudocode statements for alternation:
If ( salary > BONUS_LEVEL ) then ç One-tailed
Compute bonus
Endif
If ( endBalance < MIN_BALANCE ) then ç Two-tailed
Compute highInterestRate
Else
Compute lowInterestRate
Endif
Case ( employeeShift ) ç Multi-tailed
( day )
Set employeeOvertime to 0
( swing )
Compute employeeOvertime with SWING_BONUS
( grave )
Compute employeeOvertime with GRAVE_BONUS
Endcase
If ( employeeShift = day ) then ç Multi-tailed
Set employeeOvertime to 0
Else If ( employeeShift = swing ) then
Compute employeeOvertime with SWING_BONUS
Else
Compute employeeOvertime with GRAVE_BONUS
Endif
Endif
The following is an example of program control structures nested within other program
control structures. This structured programming technique is called nested alternation.
If ( sideA > sideB ) then
If ( sideA > sideC ) then
Set largest to sideA
Else
Set largest to sideC
Endif
Else
If ( sideB > sideC ) then
Set largest to sideB
Else
Set largest to sideC
Endif
Endif
The following is another attempt to implement the same logic, but this example is a poor
control structure design, in that:
Compound conditions complicate and even obscure the logic
Separate program control structures imply independent logic when that is not the case
Insufficient endifs close the control structures
AND THE LOGIC IS FAULTY
If ( sideA > sideB and sideB > sideC ) then
set largest to sideA
Endif
If ( sideB > sideA and sideA > sideC ) then
set largest to sideB
Endif
If ( sideC > sideB and sideB > sideA ) then
set largest to sideC
Endif
REPETITION OPERATIONS
Three types of repetition are commonly used in procedural programming:

• Repetition an unknown number of times, perhaps not at all / pre-test loop ( while )

• Repetition an unknown number of times, but at least once / post-test loop ( repeat
until / do while)

• Repetition a known number of times / counter-controlled loop ( for / do )


Here are some sample pseudo code statements for repetition:
While Not EOF( Employees )
Read employeeRecord from Employees
Compute employeePay using employeeRate, employeeHours
Write employeeName, employeeId, employeePay with labels to
printer
Endwhile
While ( choice is not valid )
Display selection menu
Read choice
If ( choice is invalid ) then
Display advisory message on screen
Endif
Endwhile
For accountNumber = 1 to numberOfAccounts
Display accountNumber with labels
If( accountBalance[accountNumber] < 0 ) then
Display overdrawn message
Else
Compute interestPayment using
accountBalance[accountNumber], interestRate
Display interestPayment
Endif
Endfor
MODULE DEFINITION AND ACTIVATION
Modules are implemented with various mechanisms in different programming languages.
Regardless of the implementations, the concept of a module is the same: an independent
block of instructions that may receive and return data. Modules may be activated in two
ways 1) procedure activation and 2) function activation.
Procedure activation appears on a line by itself in high-level programming languages and
in pseudocode. In pseudocode, the procedure activation is associated with a keyword
such as Perform, Call, or Activate.
Function activation is done in high-level programming languages by simply using the
function, within a statement, as an expression. In pseudocode, the built-in or pre-defined
functions that are a standard part of programming may be "built-in" to the pseudocode.
However, programmer-defined functions should be activated just like procedure
activations so that attention is focused upon them.
Here are some sample module activations:
A standard function in VISUAL BASIC and C++ is ABS, which returns the absolute
value of a number:
Set rate to ABS( scaleFactor )
A programmer-defined function might be written to compute a monthly payment:
Call computePayment using balance, interestRate returning
monthlyPayment
An alternate syntax for programmer-defined function calls is:
Call computePayment (balance, interestRate, monthlyPayment)
A programmer-defined module named Tally might be written to compute the sum,
average, and standard deviation of a vector of real numbers.
For index = 1 to numberOfValues
Read x(index) from inFile
Endfor
Call Tally using x
Print sum with label
Print average with label
Print stdDev with label
Module definitions must be written for all programmer-defined modules, regardless of
whether they are procedure activated or function activated. Module definitions must
include:

• Module name

• Input, inout, and output parameters

• Local variables and constants

• Pseudo code for program structures within the module


There are two alternate, acceptable formats for describing modules.
Parameter Style
Module loadList (in: fileName, out: dataList)
Local: inStream, index

Open inStream using fileName


Initialize inStream for input
Set index to 0
While not eof( inStream )
Add 1 to index
Read dataArray( index ) from inStream
Endwhile
Close inStream
Return dataList

Module getChoice (out: choice)


Local: set of valid choices

Display selection menu


Repeat
Prompt for choice
Read choice from keyboard
Convert choice to upper case
Until ( choice in set of valid choices )
Return choice

Module: selectDevice (out: outDevice)


Local: selection, fileName, set of valid choices

Prompt operator, output to screen, printer, or file


Repeat
Read selection
Convert selection to upper case
If ( selection invalid ) then
Display advisory message to screen
Endif
Until ( selection is valid )
Case ( selection )
( Screen )
assign outDevice
( Printer )
assign outDevice
( File )
Prompt for fileName
Read fileName
Assign outDevice to fileName
Endcase
Block Style
Module loadList
Givens: fileName
Results: dataList (contains dataArray and dataCount)
Local: inStream, index
Processing:
opens input stream using fileName
loads dataList from file
closes file

Open inStream using fileName


Initialize inStream for input
Set index to 0
While not eof( inStream )
Add 1 to index
Read dataArray( index ) from inStream
Endwhile
Close inStream
Return dataList
Module getChoice
Givens: none
Results: choice
Local: set of valid choices
Processing:
displays selection menu
prompts for choice
reads choice
converts choice to upper case until choice is valid
Display selection menu
Repeat
Prompt for choice
Read choice from keyboard
Convert choice to upper case
Until ( choice in set of valid choices )
Return choice
Module: selectDevice
Givens: none
Results: outDevice
Local: selection, fileName, set of valid choices
Processing:
prompts operator - output to screen, print or file
reads selection
converts selection to upper case until selection is
valid
assign outDevice according to selection
Prompt operator, output to screen, printer, or file

Repeat
Read selection
Convert selection to upper case
If ( selection invalid ) then
Display advisory message to screen
Endif
Until ( selection is valid )

Case ( selection )
( Screen )
Assign outDevice
( Printer )
Assign outDevice
( File )
Prompt for fileName
Read fileName
Assign outDevice to fileName
Endcases

You might also like