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. Flowcharts can be thought of as a graphical form of pseudocode. 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
|
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.
Program flow:
- 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
|
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
|
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
|
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
|
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
|
The otherwise clause is optional, and the same sequence may be associated with more than one condition.
|
case grade of
|
For - this construct (often called a counting loop) causes a loop to be repeated a specific number of times. The general form is:
|
for iteration bounds
|
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
|
Invoking sub-procedures
Use the call keyword, e.g.
|
CALL AvgAge with StudentAges
|