Pseudocode is an informal high-level description of an algorithm's operation that uses the structural conventions of a programming language for human understanding rather than machine execution. It omits unnecessary details like variable declarations. Pseudocode has no standard syntax but borrows from languages like Pascal, C, and Fortran. It is commonly used in textbooks and publications to document algorithms, and by programmers to plan programs before coding. Pseudocode resembles but should not be confused with executable skeleton or dummy code.
1. Pseudocode
From Wikipedia, the free encyclopedia
Jump to: navigation, search
In computer science and numerical computation, pseudocode is an informal high-level
description of the operating principle of a computer program or other algorithm. It uses the
structural conventions of a programming language, but is intended for human reading rather than
machine reading. Pseudocode typically omits details that are not essential for human
understanding of the algorithm, such as variable declarations, system-specific code and some
subroutines. The programming language is augmented with natural language description details,
where convenient, or with compact mathematical notation. The purpose of using pseudocode is
that it is easier for people to understand than conventional programming language code, and that
it is an efficient and environment-independent description of the key principles of an algorithm.
It is commonly used in textbooks and scientific publications that are documenting various
algorithms, and also in planning of computer program development, for sketching out the
structure of the program before the actual coding takes place.
No standard for pseudocode syntax exists, as a program in pseudocode is not an executable
program. Pseudocode resembles, but should not be confused with skeleton programs, including
dummy code, which can be compiled without errors. Flowcharts and UML charts can be thought
of as a graphical alternative to pseudocode, but are more spacious on paper.
Contents
1 Application
2 Syntax
3 Mathematical style pseudocode
4 Machine compilation of pseudo-code style languages
o 4.1 Natural language grammar in programming languages
o 4.2 Mathematical programming languages
4.2.1 Alternative forms of pseudocode
5 See also
6 External links
Application
Textbooks and scientific publications related to computer science and numerical computation
often use pseudocode in description of algorithms, so that all programmers can understand them,
even if they do not all know the same programming languages. In textbooks, there is usually an
accompanying introduction explaining the particular conventions in use. The level of detail of
the pseudo-code may in some cases approach that of formalized general-purpose languages.
2. A programmer who needs to implement a specific algorithm, especially an unfamiliar one, will
often start with a pseudocode description, and then "translate" that description into the target
programming language and modify it to interact correctly with the rest of the program.
Programmers may also start a project by sketching out the code in pseudocode on paper before
writing it in its actual language, as a top-down structuring approach.
Syntax
As the name suggests, pseudocode generally does not actually obey the syntax rules of any
particular language; there is no systematic standard form, although any particular writer will
generally borrow style and syntax; for example, control structures from some conventional
programming language. Popular syntax sources include Pascal, BASIC, C, C++, Java, Lisp, and
ALGOL. Variable declarations are typically omitted. Function calls and blocks of code, such as
code contained within a loop, are often replaced by a one-line natural language sentence.
Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact
imitation of a real programming language at one extreme, to a description approaching formatted
prose at the other.
This is an example of pseudocode (for the mathematical game bizz buzz):
Fortran style pseudo code Pascal style pseudo code C style pseudo code:
void function bizzbuzz
program bizzbuzz procedure bizzbuzz
for (i = 1; i<=100; i++)
do i = 1 to 100 for i := 1 to 100 do
{
set print_number to set print_number to
set print_number to
true true;
true;
if i is divisible by if i is divisible by
if i is divisible by
3 3 then
3
print "Bizz" print "Bizz";
print "Bizz";
set print_number set print_number
set print_number
to false to false;
to false;
if i is divisible by if i is divisible by
if i is divisible by
5 5 then
5
print "Buzz" print "Buzz";
print "Buzz";
set print_number set print_number
set print_number
to false to false;
to false;
if print_number, if print_number,
if print_number,
print i print i;
print i;
print a newline print a newline;
print a newline;
end do end
}
See also category: Articles with example pseudocode
Mathematical style pseudocode
In numerical computation, pseudocode often consists of mathematical notation, typically from
set and matrix theory, mixed with the control structures of a conventional programming
3. language, and perhaps also natural language descriptions. This is a compact and often informal
notation that can be understood by a wide range of mathematically trained people, and is
frequently used as a way to describe mathematical algorithms. For example, the sum operator
(capital-sigma notation) or the product operator (capital-pi notation) may represent a for loop and
perhaps a selection structure in one expression:
Return
Normally non-ASCII typesetting is used for the mathematical equations, for example by means
of TeX or MathML markup, or proprietary formula editors.
These are examples of articles that contain mathematical style pseudo code:
Algorithm Karmarkar's algorithm
Conjugate gradient method Particle swarm optimization
Ford-Fulkerson algorithm Stone method
Gauss–Seidel method Successive over-relaxation
Generalized minimal Symbolic Cholesky decomposition
residual method Tridiagonal matrix algorithm
Jacobi eigenvalue
algorithm
Jacobi method
Mathematical style pseudocode is sometimes referred to as pidgin code, for example pidgin
ALGOL (the origin of the concept), pidgin Fortran, pidgin BASIC, pidgin Pascal, pidgin C, and
pidgin Lisp.
Machine compilation of pseudo-code style languages
Natural language grammar in programming languages
Various attempts to bring elements of natural language grammar into computer programming
have produced programming languages such as HyperTalk, Lingo, AppleScript, SQL, Inform
and to some extent Python. In these languages, parentheses and other special characters are
replaced by prepositions, resulting in quite talkative code. These languages are typically
dynamically typed, meaning that variable declarations and other boilerplate code can be omitted.
Such languages may make it easier for a person without knowledge about the language to
understand the code and perhaps also to learn the language. However, the similarity to natural
language is usually more cosmetic than genuine. The syntax rules may be just as strict and
formal as in conventional programming, and do not necessarily make development of the
programs easier.
Mathematical programming languages
4. An alternative to using mathematical pseudocode (involving set theory notation or matrix
operations) for documentation of algorithms is to use a formal mathematical programming
language that is a mix of non-ASCII mathematical notation and program control structures. Then
the code can be parsed and interpreted by a machine.
Several formal specification languages include set theory notation using special characters.
Examples are:
Z notation
Vienna Development Method Specification Language (VDM-SL).
Some array programming languages include vectorized expressions and matrix operations as
non-ASCII formulas, mixed with conventional control structures. Examples are:
A programming language (APL), and its dialects APLX and A+.
Pseudocode Examples
An algorithm is a procedure for solving a problem in terms of the actions to be
executed and the order in which those actions are to be executed. An algorithm is
merely the sequence of steps taken to solve a problem. The steps are normally
"sequence," "selection, " "iteration," and a case-type statement.
In C, "sequence statements" are imperatives. The "selection" is the "if then else"
statement, and the iteration is satisfied by a number of statements, such as the
"while," " do," and the "for," while the case-type statement is satisfied by the
"switch" statement.
Pseudocode is an artificial and informal language that helps programmers develop
algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool.
The rules of Pseudocode are reasonably straightforward. All statements showing
"dependency" are to be indented. These include while, do, for, if, switch. Examples
below will illustrate this notion.
What is pseudocode?
Pseudocode consists of short, English phrases used to explain specific tasks within a
program's algorithm. Pseudocode should not include keywords in any specific computer
languages. It should be written as a list of consecutive phrases. You should not use
flowcharting symbols but you can draw arrows to show looping processes. Indentation
can be used to show the logic in pseudocode as well. For example, a first-year, 9th grade
Visual Basic programmer should be able to read and understand the pseudocode written
by a 12th grade AP Data Structures student. In fact, the VB programmer could take the
other student's pseudocode and generate a VB program based on that pseudocode.
Why is pseudocode necessary?
The programming process is a complicated one. You must first understand the program
specifications, of course, Then you need to organize your thoughts and create the
program. This is a difficult task when the program is not trivial (i.e. easy). You must
break the main tasks that must be accomplished into smaller ones in order to be able to
eventually write fully developed code. Writing pseudocode WILL save you time later
during the construction & testing phase of a program's development.
5. How do I write pseudocode?
First you may want to make a list of the main tasks that must be accomplished on a piece
of scratch paper. Then, focus on each of those tasks. Generally, you should try to break
each main task down into very small tasks that can each be explained with a short phrase.
There may eventually be a one-to-one correlation between the lines of pseudocode and
the lines of the code that you write after you have finished pseudocoding.
It is not necessary in pseudocode to mention the need to declare variables. It is wise
however to show the initialization of variables. You can use variable names in
pseudocode but it is not necessary to be that specific. The word "Display" is used in some
of the examples. This is usually general enough but if the task of printing to a printer, for
example, is algorithmically different from printing to the screen, you may make mention
of this in the pseudocode. You may show functions and procedures within pseudocode
but this is not always necessary either. Overall, remember that the purpose of pseudocode
is to help the programmer efficiently write code. Therefore, you must honestly attempt to
add enough detail and analysis to the pseudocode. In the professional programming
world, workers who write pseudocode are often not the same people that write the actual
code for a program. In fact, sometimes the person who writes the pseudocode does not
know beforehand what programming language will be used to eventually write the
program.
Example:
Original Program Specification:
Write a program that obtains two integer numbers from the user. It will print out the sum
of those numbers.
Pseudocode:
Prompt the user to enter the first integer
Prompt the user to enter a second integer
Compute the sum of the two user inputs
Display an output prompt that explains the answer as the sum
Display the result
Use this template when typing your pseudocode into MS Word: