Pseudocode
Pseudocode is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient.
Pseudocode can perhaps be thought of as the text version of a flowchart. As the name suggests, pseudocode does not need to obey the syntax rules of a specific programming language, and there is no standard format, although the programmer may imitate the appearance of a particular programming language.
Details not relevant to an algorithm are usually omitted. Blocks of code (for example that contained within a loop) may be described in a single natural language sentence. Pseudocode can thus vary widely in style. Programming textbooks often use pseudocode to describe algorithms so that all programmers can understand them, whatever programming language they use, although the conventions used are usually clearly defined. A programmer will often begin with a pseudocode description of a program or algorithm, and then simply translate this into their chosen programming language.
A pseudocode example:
if credit card number is valid
execute transaction based on number and order
else
show a generic failure message
end if
A Pseudocode Standard
Pseudocode is a kind of structured English for describing algorithms that allows the programmer to focus on the logic of the algorithm without being distracted by the details of programming language syntax. Writing the program is then simply a matter of translating the pseudocode into source code.
The vocabulary used is usually that of the problem domain, with the only stipulation being that the logic must be specified in sufficient detail to allow the source code to be derived without breaking the problem down further. There is no universal standard for pseudocode, but it is helpful to follow certain conventions, some of which are described below.
Controlling the flow of program execution:
- sequence - a linear progression where tasks are performed sequentially
- if then else - a choice is made between two alternative courses of action
- while - a loop with a simple conditional test at the beginning
- repeat until - a loop with a simple conditional test at the bottom
- case - choose from several actions, depending on the value of an expression
- for - a counting loop
Sequence - a sequence is indicated by writing one action after another, each on a separate line, and each at the same level of indentation. The actions are performed in the order in which they appear, e.g.
READ height of rectangle
READ width of rectangle
COMPUTE area as height times width
If then else - this construct defines a simple two-way choice based on whether or not a condition equates to true or false, e.g.
if hoursWorked > normalMax then
display overtime message
else
display regular time message
end if
While - this construct specifies a loop with a test at the beginning. The loop is only entered if the condition is true, and the specified action (or sequence)is performed for each iteration of the loop. The condition is evaluated before each iteration.
while population < limit
compute population as population + births - deaths
end while
Repeat until - similar to the while loop, except that the test is performed at the end of the loop, so that the specified sequence will be performed at least once. The loop repeats if the condition is false, and terminates when it becomes true. The general form is:
repeat
sequence
until condition
Case - this construct allows the program to execute one of a number of alternative sequences, depending on which of a set of mutually exclusive conditions is true. It takes the form:
case expression of
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
otherwise:
default sequence
end case
The otherwise clause is optional, and the same sequence may be associated with more than one condition.
case grade of
A: points = 4
B: points = 3
C: points = 2
D: points = 1
E: points = 0
end case
For - often called a counting loop, this construct causes a loop to be repeated a specific number of times. Here is the general form of the construct:
for iteration bounds
sequence
end for
Nested constructs
Constructs can be nested inside each other, and this should be made clear by appropriate use of indentation, e.g.
set total to zero
repeat
read temperature
if temperature > freezing then
increment total
end if
until temperature < zero
print total
Invoking sub-procedures
Use the call keyword, e.g.
CALL AvgAge with StudentAges
CALL Swap with CurrentItem and TargetItem
CALL Account.debit with CheckAmount
CALL getBalance RETURNING aBalance