

![]()
The algorithms do three types of operations:
- entry/exit
- attributing
- decision
The algorithm makes this operations in order to transform the given data into requested data.
Generally, algorithms are made by the exclusive utilization of certain structures ( linear, alternative, repetitive ).
A structure is a way of combining the operations an algorithm is working with.
The linear structure
A linear structure is a chain of operations that are executed in the order of their apparition:
op1
op2
op3
.
.
opn
The alternative structure
The alternative structure is a structure that permits the execution of a certain instruction if a condition is satisfied.
If S1 and S2 are structures and E is a logical expression, the following is an alternative structure:
if E then S1
else S2
endif
The execution mechanism is:
- evaluating the logical expression
-if the value of the expression is true, the structure S1 is being executed, else the structure S2 is being executed.
Example: Given an integer number n, find out if it is a multiple of 2.
Pascal version:
var n : integer;
begin
readln(n);
if n mod 2=0 then writeln(n, ' is a multiple of 2 ')
else writeln(n,' is not a multiple of 2 ')
end.
C++ version:
#include <iostream.h>
void main()
{
int n;
cin>>n;
if (n%2= =0){cout<<n<<" is a multiple of 2"<<endl;}
else {cout<<n<<"is not a multiple of 2"<<endl;}
}
The repetitive structure
There are two types of repetitive structures:
1. with initial condition:
while <condition> do <operation>
2. with final condition:
repeat <operation> until <condition>
Example: Write the first n natural numbers.
Pascal version:
var i,n:integer;
begin
readln(n);
for i:=0 to n-1 do
write(i,' ');
writeln;
end.
or
var i,n:integer;
begin
readln(n);
i=0;
while i<>n do
begin
write(i,' ');
inc(i);
end;
writeln;
end.
or
var n,i:integer;
begin
readln(n);
i:=0;
repeat
write(i,' ');
inc(i);
until i=n;
end.
C++ version:
#include<iostream.h>
void main()
{
int n,i;
cin>>n;
i=0;
while (i<n)
{
cout>>i>>endl;
i+=1;
}
}
or
#include<iostream.h>
void main()
{
int n,i;
cin>>n;
for (i=0;i<n;i++)
cout<<i<<endl;
}
or
#include<iostream.h>
void main()
{
int n,i;
cin>>n;
i=0;
do
{
cout>>i>>endl;
i+=1;
}
while (i!=n)
}