You are on page 1of 4

10

Programs and
pseudocode algorithms

The concept of a program


A program is a sequence of instructions or programming language statements written to make a
computer perform certain tasks.
Well-structured programs require a programming language to support the following
programconstructs:
sequence
selection
iteration
subroutine.
A computers processor can only run a computer program in the form of a file of machine code,
which is a sequence of binary codes representing instructions for the processor.
The instruction set for a family of processors is the machine language in which machine code is
written for that family of processors.
When machine code runs, the processor repeatedly:
fetches an instruction from internal memory
decodes the instruction
executes the instruction.

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.

Cambridge IGCSE Computer Studies

Cambridge University Press 2011

Revision notes: 10 Programs and pseudocode algorithms

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

Use the arithmetic operators in Table 10.4 of the coursebook.


Assignment

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.

Cambridge IGCSE Computer Studies

Cambridge University Press 2011

Revision notes: 10 Programs and pseudocode algorithms

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

It is sometimes necessary to count how many times something happens.


To count up or increment by 1, we can use statements such as:
Count Count + 1
increment Count by 1

Structured statements for iteration (repetition or loops)

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).

Producing algorithms in pseudocode


Writing an algorithm in pseudocode is no longer graphical like a program flowchart, but is one
step closer to writing program code in a high-level language.
Producing an algorithm for a solution in pseudocode typically includes:
initialising any variables for totalling and counting
using REPEATUNTIL for input validation
using an appropriate loop structure for repetitions of data entry and/or other processing
using conditional statements to select appropriate processing alternatives
IFTHENELSEENDIF statements for adjusting the values of maximum and minimum
variables cannot be nested.

Testing and interpreting pseudocode algorithms


Dry running a pseudocode algorithm with a trace table and test data helps to understand its
behaviour and purpose.

Cambridge IGCSE Computer Studies

Cambridge University Press 2011

Revision notes: 10 Programs and pseudocode algorithms

Example problem
Line 1 Exponent 0
Line 2 REPEAT
Line 3

Result 2 ^ Exponent

Line 4

PRINT "2^", Exponent, " = ", Result

Line 5

Exponent Exponent + 1

Line 6 UNTIL Result > 100


Figure 10.11 What does this pseudocode algorithm do?

Example trace table


Table 10.7 Trace table for pseudocode in Figure 10.11.

Result

PRINT

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.

Finding and correcting errors in pseudocode algorithms


It is important to be able to identify errors and suggest corrections in a pseudocode algorithm.
Common errors in pseudocode algorithms include:
missing or faulty initialisation of variables
faulty initial and final values for the control variable (loop counter) in a FORTONEXT loop
incrementing the loop counter in a FORTONEXT loop, which interferes with the
automaticcounting
failing to increment an optional counter variable in the other sorts of loop
failing to complete a structured statement with the requisite ending keyword ENDWHILE, UNTIL,
NEXT, ENDIF or ENDCASE
misplacing a keyword, so that statements are inappropriately inside or outside a loop.

Cambridge IGCSE Computer Studies

Cambridge University Press 2011

Revision notes: 10 Programs and pseudocode algorithms

You might also like