Professional Documents
Culture Documents
Programs and
pseudocode algorithms
Programming languages
Computer programming languages can be divided into two types:
low-level closer to machine language
high-level closer to English.
Low-level languages
Machine language is the lowest-level sort of language, because the long string of zeros and ones in
a machine code program is so difficult for a programmer to read or write.
Each complete machine language instruction consists of an opcode (operation code), which may
be as short as a single byte, even for a 32- or 64-bit processor.
Some opcodes require one or more operands, which are numerical values or memory addresses
upon which they operate.
An assembly language for each family of processor is an alternative to machine language. Assembly
code is easier to read and write than machine code, as two or more meaningful letters act as a
mnemonic (memory-aid) for each machine language opcode.
Assembly code (source code) must be translated into machine code (object code). This can be
done manually, but it is quicker and more reliable to use a translation program called an assembler.
High-level languages
High-level programming languages look far more like English or mathematical notation than
low-level languages. They are much easier to read, write, test, debug and maintain, are problemoriented rather than machine-oriented, and are portable between different families of processor.
A high-level language program must be translated into machine code before it can be run by a
processor. This needs translation software that is more complex than an assembler, since one
high-level language statement is translated into one or more machine language instructions.
A high-level language program can be translated in two very different ways:
by a compiler program
by an interpreter program.
Pseudocode
Pseudocode uses keywords commonly found in high-level languages and mathematical notation.
It describes an algorithms steps like program statements, without being bound by the strict rules
of vocabulary and syntax of any particular language, together with ordinary English.
Arithmetic
Assignment is the process of writing a value into a variable (a named memory location). For
example, Count 1 can be read as Count is assigned the value 1, Count is made equal to 1 or
Count becomes 1. Another way of indicating assignment is a pseudocode statement suchas:
set Swapped to False
If an algorithm needs to read the value of a variable before it assigns input data or a calculated
value to the variable, the algorithm should assign an appropriate initial value to the variable,
known as initialisation.
Input
We indicate input by words such as INPUT, READ or ENTER, followed by the name of a variable to
which we wish to assign the input value.
Output
We indicate output by words such as OUTPUT, WRITE or PRINT, followed by a comma-separated list
of expressions.
Totalling
To keep a running total, we can use a variable such as Total or Sum to hold the running total and
assignment statements such as:
Total Total + Number
add Number to Total
Counting
Many problems involve repeating one or more statements, so it is useful to have structured
statements for controlling these iterations or repetitions. Exit conditions consist of logical
expressions whose truth can be tested, such as Count = 10 or Score < 0. At a particular time, a
logical expression is either True or False.
WHILEDOENDWHILE (see, for example, Figure 10.2 in the coursebook)
REPEATUNTIL (see, for example, Figure 10.3 in the coursebook)
FORTONEXT (see, for example, Figure 10.5 in the coursebook).
Structured statements for selection (conditional statements)
These statements are used to select alternative routes through an algorithm, using logical
expressions as conditions for the decisions involved. As with iteration (repetition), selections
logical expressions often involve comparisons, which can operate on text strings as well as
numbers (see Table 10.5 in the coursebook).
IFTHENELSEENDIF (see, for example, Figure 10.7 in the coursebook)
CASEOFOTHERWISEENDCASE (see, for example, Figure 10.8 in the coursebook).
Example problem
Line 1 Exponent 0
Line 2 REPEAT
Line 3
Result 2 ^ Exponent
Line 4
Line 5
Exponent Exponent + 1
Result
Exponent
Notes
Initialise variable
2^0 = 1
1st iteration
2^1 = 2
2nd iteration
2^2 = 4
3rd iteration
2^3 = 8
4th iteration
16
2^4 = 16
5th iteration
32
2^5 = 32
6th iteration
64
2^6 = 64
7th iteration
128
2^7 = 128
8th iteration
Example interpretation
The purpose of the algorithm is to print a list of the powers of 2 starting at 20 until it reaches the
first one over 100.