indexed pseudo code
TRANSCRIPT
-
8/10/2019 Indexed Pseudo Code
1/21
A Pseudo-CodeStandard
byJ F Pratt
-
8/10/2019 Indexed Pseudo Code
2/21
1A Pseudo-Code Standard
What is Pseudo-Code ?Pseudo-Code is a programming-like
formal form of English, that can be usedto 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);
-
8/10/2019 Indexed Pseudo Code
3/21
2A Pseudo-Code Standard
Pseudo-Code StandardWhen using pseudo-code it is useful for a
programming community to stick to astandard way of expressing code which
is programming language independent.
Meta-Names
A name in angled brackets eg
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 definethe correct format (syntax) of pseudo-code
statements. Their meaning (semantics) is
what they do.
-
8/10/2019 Indexed Pseudo Code
4/21
-
8/10/2019 Indexed Pseudo Code
5/21
4A Pseudo-Code Standard
More Assignments
(ii)
x = y + 2 ; means put the value of y + 2
into x.
x 12 y 3
y + 2
|
3 + 2 = 5
x 5 y 3
(iii) x = x + 1 ; means add one to x.
x 5 x 6
-
8/10/2019 Indexed Pseudo Code
6/21
5A Pseudo-Code Standard
Operators
Some standard operators that can be used
in expressions, are given below. Use BODMAS
- School arithmetic - to evaluate expressions.
( ) brackets highest
* multiplication medium
/ division
+ addition lowest- subtraction
Examples
a 3 b 4
(i) b + a * 4 = 4 + 12 = 16
(ii) ( b + a ) * 4 = 7 * 4 = 28
Operator Usage Priority
-
8/10/2019 Indexed Pseudo Code
7/21
6A Pseudo-Code Standard
I/O : Input Statement
Assign value(s) from the keyboard to
the variable(s) in the order given. This
is an example of an input statement.
Example
read( a, b, c ) ;
Get three values from the keyboard,
the first goes into a, the second goes into b,and the third one goes into c.
User types : 3 4 5
a 3 b 4 c 5
read();
-
8/10/2019 Indexed Pseudo Code
8/21
7A Pseudo-Code Standard
I/O : Output Statement
Print out the value(s) of the expression(s)
on the screen in the order given. This is
an example of an output statement.
Example
write( the answer is , a+2) ;
Supposing a were currently 42, then wewould get as output.
the answer is 44
write();
-
8/10/2019 Indexed Pseudo Code
9/21
8A Pseudo-Code Standard
Constructs
Constructs build simple statements such
as assignment and I/O into higher levelstatements.
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.
-
8/10/2019 Indexed Pseudo Code
10/21
9A 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.
NB______ stands for assignment,
I/O or a construct.
-
8/10/2019 Indexed Pseudo Code
11/21
10A Pseudo-Code Standard
Comparisons
In the constructs that follow often we will need
to compare one value with another, namely, werequire simple conditions called comparisons
of the form :-
value1 value2
We give below a table of the various relational
operators that are available for comparing
values.
Comparisons evaluate to either trueor false.
Symbol Relational Operator
< strictly less than
= greater than or equal to
> strictly greater than
! = not equal to
-
8/10/2019 Indexed Pseudo Code
12/21
11A Pseudo-Code Standard
Comparison Examples
n 4 m 3
m + 1 = = n is true
m != n is truen != m + 1 is false
mn = = 0 is false
Compound Conditions
More complex conditions can be tested for byusing and, or, not. With andall subconditions
have to be truefor the condition to be true.
With orat least one of the subconditions needs
to be truefor the condition to be true. With
notthe subcondition needs to be false if thecondition overall is to betrue.
eg
not( m = = n and n != m+1) is true
-
8/10/2019 Indexed Pseudo Code
13/21
12A Pseudo-Code Standard
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
The first idiom is distinct from the other
two, in that the option of doing nothingis available.
-
8/10/2019 Indexed Pseudo Code
14/21
13A Pseudo-Code Standard
One-way Choice
if 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
swap a and b.
-
8/10/2019 Indexed Pseudo Code
15/21
14A Pseudo-Code Standard
Two-way Choice
if then______;
______;
______;
else
______;______;
______;
end if
Execute the first arm (the thenpart) providedthe condition is true, otherwise execute the
second arm (the elsepart).
if height > 1.42 then
write( ok to go on ride);
else
write(sorry youre not tall enough);
end if
Example
-
8/10/2019 Indexed Pseudo Code
16/21
15A Pseudo-Code Standard
Multi-way Choice
if then______;
______;
______;
else if
______;______;
______;
else
______;
______;______;
end if
Execute the first arm (the thenpart) if
condition 1 is true, otherwise execute the
second arm if condition 2 is true,
otherwise execute the third arm.
NBExtra else if s can
be added, as necessary.
NBno
condition
here.
-
8/10/2019 Indexed Pseudo Code
17/21
16A Pseudo-Code Standard
Iteration Constructs
While loop
while do
______ ;
______ ;
______ ;
end while
An unpredictable - non-deterministic loop.
For loop
for = todo
______ ;______ ;
______ ;
end for
A predictabledeterministic loop.
Execute the statements repeatedly providing
the condition is trueor ,in the case of for loops,
providing the for loop variable is within range.
-
8/10/2019 Indexed Pseudo Code
18/21
17A Pseudo-Code Standard
A While Example
Sum a negatively terminated list of integers.
egFor the data set :-
1 2 3 -11
the sum of the positives is 6.
The negative value -11in this case simply
signals the end of the positive integers -
playing the role of a sentinel.
sum=
0;read(x);
while x >= 0 do
sum = sum + x ;
read(x) ;
end whilewrite(total is ,sum);
-
8/10/2019 Indexed Pseudo Code
19/21
18A Pseudo-Code Standard
A For ExampleProduce a table of squares and cubes, of
the integers from 1 to n inclusively.
read(n);
for i = 1 ton do
write(i); write (i2
); write (i3
);write();
end for
NBalthough some programming languages
provide powers, in others we might have touse i i to get i2 and likewise for i3.
NBWe put downin front of the tokeyword
to get a downward counting for loop.
*
1 1 1
2 4 8
.
n n2 n3
-
8/10/2019 Indexed Pseudo Code
20/21
Index
2
5
6
7
1
3
4
8
What is Pseudo-Code ?
Pseudo-Code Standard
Assignment Statement
More Assignments
Operators
I/O : Input Statement
I/O : Output Statement
Constructs
Sequence Construct
Front Page0
9
-
8/10/2019 Indexed Pseudo Code
21/21
Index
11
12
13
15
16
14
17
18
10
Comparison Examples
Selection Constructs
One-way Choice
Two-way Choice
Multi-way Choice
Iteration Constructs
A While Example
A For Example
Comparisons