Professional Documents
Culture Documents
Standard
by
J F Pratt
What is Pseudo-Code ?
Pseudo-Code is a programming-like
formal form of English, that can be used
to express a programming strategy or
algorithm, namely, a recipe to solve
a problem.
Problem
Find the mean of two numbers.
Pseudo-Code
float
a, b, mean;
read (a, b) ;
mean = ( a + b ) / 2 ;
write( when a = , a, and b = , b ) ;
write( their mean is , mean);
A Pseudo-Code Standard
Pseudo-Code Standard
When using pseudo-code it is useful for a
programming community to stick to a
standard way of expressing code which
is programming language independent.
Meta-Names
A name in angled brackets eg <variable>
means that an instance of the indicated
kind needs to be supplied.
Symbols
+ , - , / , * , <= , >= , =, . . . and so on
are written literally as shown.
Syntax
Meta-names and symbols are used to define
the correct format (syntax) of pseudo-code
statements. Their meaning (semantics) is
what they do.
A Pseudo-Code Standard
Assignment Statement
<variable> = <expression> ;
Work out the expression on the right and
overwrite the variable on the left with the
result of evaluating the expression.
Examples
(i) x is an instance of a <variable>
2 is an instance of an <expression>
so the statement below is a legal
assignment statement.
x=2;
It means simply assigns x the value 2.
A Pseudo-Code Standard
More Assignments
(ii)
x = y+2;
12
y
|
3
y
+
= 5
y
A Pseudo-Code Standard
Operators
Some standard operators that can be used
in expressions, are given below. Use BODMAS
- School arithmetic - to evaluate expressions.
Operator
Usage
Priority
()
brackets
highest
*
/
multiplication
division
medium
+
-
addition
subtraction
lowest
Examples
a
(i)
b + a * 4 = 4 + 12 = 16
(ii)
( b+a)*4
A Pseudo-Code Standard
= 7 * 4 = 28
5
A Pseudo-Code Standard
A Pseudo-Code Standard
Constructs
Constructs build simple statements such
as assignment and I/O into higher level
statements.
They have one entry point and one exit
point - this is important for producing
well-structured (easily maintained) code.
The Three Fundamental Constructs
- Sequence
- Selection
- Iteration
There are several selection constructs
and several iteration constructs available,
the ones considered here are sufficient
for most purposes.
A Pseudo-Code Standard
Sequence Construct
______;
______;
______;
Execute the statements in order, namely
top to bottom and left to right.
Example
temp = x ;
x
= y;
y
= temp ;
These three assignment statements mean
swap x and y.
A Pseudo-Code Standard
Comparisons
In the constructs that follow often we will need
to compare one value with another, namely, we
require simple conditions called comparisons
of the form :value1 <relational operator> value2
We give below a table of the various relational
operators that are available for comparing
values.
Symbol
<
<=
==
>=
>
!=
Relational Operator
strictly less than
less than or equal to
equal to
greater than or equal to
strictly greater than
not equal to
10
Comparison Examples
n
m+1
m
n
mn
==
!=
!=
==
n
n
m+1
0
is true
is true
is false
is false
Compound Conditions
More complex conditions can be tested for by
using and, or, not. With and all subconditions
have to be true for the condition to be true.
With or at least one of the subconditions needs
to be true for the condition to be true. With
not the subcondition needs to be false if the
condition overall is to be true.
eg
not ( m = = n and n != m+1) is true
A Pseudo-Code Standard
11
Selection Constructs
Selection constructs are used to make decisions
in programs, there are three main idioms.
- One-way choice
- Two-way choice
- Multi-way choice
A Pseudo-Code Standard
12
One-way Choice
if <condition> then
______;
______;
______;
end if
Execute the first arm provided the condition
is true.This means do nothing when the
condition is false.
Example
Perform a conditional swap to order a and b.
if a > b then
temp = a;
a
= b;
b
= temp;
end if
A Pseudo-Code Standard
swap a and b.
13
Two-way Choice
if <condition> then
______;
______;
______;
else
______;
______;
______;
end if
Execute the first arm (the then part) provided
the condition is true, otherwise execute the
second arm (the else part).
Example
if height > 1.42 then
write( ok to go on ride);
else
write(sorry youre not tall enough);
end if
A Pseudo-Code Standard
14
Multi-way Choice
if <condition 1> then
______;
______;
______;
else if <condition 2>
______;
______;
______;
else
______;
______;
______;
end if
NB no
condition
here.
15
Iteration Constructs
While loop
while <condition> do
______ ;
______ ;
______ ;
end while
An unpredictable - non-deterministic loop.
For loop
for <integer> = <start> to <finish> do
______ ;
______ ;
______ ;
end for
A predictable deterministic loop.
Execute the statements repeatedly providing
the condition is true or ,in the case of for loops,
providing the for loop variable is within range.
A Pseudo-Code Standard
16
A While Example
Sum a negatively terminated list of integers.
eg
For the data set :1 2
3
-11
the sum of the positives is 6.
A Pseudo-Code Standard
17
A For Example
Produce a table of squares and cubes, of
the integers from 1 to n inclusively.
1
2
n
1
4
.
n2
1
8
n3
read(n);
for i = 1 to n do
write(i); write (i2); write (i3);
write(<new_line>);
end for
NB although some programming languages
provide powers, in others we might have to
use i * i to get i2 and likewise for i3.
NB We put down in front of the to keyword
to get a downward counting for loop.
A Pseudo-Code Standard
18
Index
0
Front Page
What is Pseudo-Code ?
Pseudo-Code Standard
Assignment Statement
More Assignments
Operators
Constructs
Sequence Construct
Index
10
Comparisons
11
Comparison Examples
12
Selection Constructs
13
One-way Choice
14
Two-way Choice
15
Multi-way Choice
16
Iteration Constructs
17
A While Example
18
A For Example