Algorithm Constructs

An algorithm is a sequence of instructions used to solve a problem.

Note the word sequence in this definition. It means there is an order to the instructions.

 

 

Algorithm Constructs

Pseudocode is a method of writing down an algorithm. 

Pseudocode does not use the syntax of any particular programming language but it does show the structure of a solution.

It is important that 'blocks' and loops are indented to show the structure clearly.

There are not really any  conventions used in pseudocode but I would suggest using the following:

  • For any input/output, use...

input (mark)
output (
totalmark)

  • use appropriate variable names,...
  • use = to assign values...

counter = 0

 

The basic 'constructs' of an algorithm are :

Sequence : A number of instructions is processed one after the other.

eg

statement 1;
statement 2;
statement 3;

 

Pseudocode example
input(name)
input(age)
output(message)

 

Selection : The next instruction to be executed depends on a 'condition' :

if (condition is true) then
  statement 1
else  
  statement 2;
 end if  

 

Pseudocode example
input(name)
input(age)
if age < 21 then
    output(young message)
else
    output(old message)
end if

 

Iteration : A number of instructions is repeated....

(a)...a fixed number of times:

for 20 times do
statement 1;
statement 2;

end do

 

Pseudocode example
for 20 times do
    input(name)
    output(name)
end do 

[Note in the program that a loop is used to draw the line. This is an example of a 'nested loop' - a loop inside another loop]

 

(b)....until a condition is met:

  repeat
    statement 1;
    statement 2;
  until (condition is met)
or    
  while (condition is true) do
    statement 1;
    statement 2;
   end while

 

Pseudocode example
repeat
    input (mark)
    total = total + mark
until mark = 0