The Algorithmic Language
The Algorithmic Language
ENSM
2023 – 2024
Question: How to define a language which makes it possible to express the solution of
any problem? This is the objective of the algorithmic language!
1
III. The general structure of an algorithm
- An algorithm is structured in two parts contained between the keywords start and end.
Each algorithm has a name. The structure appears as follows:
algorithm <AlgoName>;
start
<part 1: declaration of variables>
end
algorithm QuadraticEquationSol;
start
a, b, c, x, x1, x2, delta: real;
read (a, b, c);
delta ¬ (b * b) – (4 * a * c);
2
Explanations:
• The name of this algorithm is “QuadraticEquationSol”.
• The line “a, b, c, x, x1, x2, delta: real;” defines six
variables. These are real numbers.
• The order “read (a, b, c);” is assumed to be known by the machine. It
allows to read the values of the variables “a, b and c”. At runtime, the user
has to provide (i.e. to enter) values of the variables. For instance, if the user
would like to solve the equation “3 * x2 + 5*x + 14 = 0”, she/he has to enter the
values 3, 5 and 14 to the variables a, b and c, respectively.
• The sequence of orders “if … then … endif” allows us to check a
condition (here, testing the value of variable “delta”).
• If we would like to solve a set of quadratic equations, we have just to repeat
the same treatment as many times as the number of equations we have.
As we have just seen in the example, the algorithmic language must allow at least to:
- Declare a variable or a set of variables;
- Read data;
- Display (print) calculated results or messages;
- Calculate arithmetic and logical expressions;
- Assign an expression value to a variable;
- Test the value of a variable;
- A treatment according to a condition;
- Repeat a treatment.
V. Declaration of a variable
A sequence of variables x1, ..., xk of the same type can be declared in a single declaration:
3
VI. Simple Types
• The “real” type is the set of real numbers. This set also supports arithmetic operations
(which are also applied to integers) and which are: +, -, *, /, **. It also
supports comparison: £, <, =, >, ³, ¹.
In a real constant, the decimal point separates the integer part from the decimal part. For
example: -3.1459, 145.5, 0, 0.75 are real constants.
The “boolean” type represents the ordered set with two values false and true. The
comparison of two integers or reals provides a “boolean” type result.
• The "character" type contains all the constants that are written with a single letter. This
letter can be for example what we type on a keyboard. It represents lowercase and
uppercase alphabetic letters ( 'a', ..., 'z', 'A', ...,'Z' ), numeric
characters ( '0', ..., '9' ) , the special characters ( '+', '*', '?', '$',
'''', ... ), and the space (or blank) character.
• The “string” type represents the set of character sequences that we can form by
juxtaposing a set of “character” type elements.
A "string" constant is written between two quotes, for example: "Bonjour!"
We can apply functions to a variable of type “string” such as:
length(s): which gives an integer result corresponding to the number of
characters that make up the string s.
For instance:
length("Hello!") returns the integer value 6.
concat(s1, s2) : which concatenates two strings and returns the result of
the concatenation. For instance:
concat("Hello ", "dear students") returns the string
"Hello dear students".
4
VII. Basic actions
• Reading data
It is expressed by the read order with the parameters which represent the variables to
be read.
If x1, ..., xk are k given variables, their reading can be done using the
following order:
• Displaying results
In a symmetrical way with respect to reading, we can perform the visualization of
results by the order:
• Assignment
x ¬ 3.14;
x ¬ <arithmetic/logical expression>;
5
VIII. Structured actions
There are two types of structured actions:
1. Those that allow the execution of a series of actions depending on a condition to be
checked (like the test on the value of delta in the example of the quadratic equation);
2. Those which make it possible to repeat a process and which we call “iterations” or
“loop”.
• Conditional Statements
They respect the writing syntax according to one of the two following forms.
1. The first form expresses a processing choice: Either we execute the sequence of
actions ActionA1, ..., ActionAm if <condition> is true, otherwise
we execute the sequence ActionB1, ..., ActionBn if the condition is
false.
algorithm QuadraticEquationSol;
start
a, b, c, x, x1, x2, delta: real;
read(a, b, c);
delta ¬ (b * b) – (4 * a * c);
end
6
• Iterative statements
Example 1:
Example 2:
s ¬ 0;
for i = 0 to 9 step 1 do
s ¬ s+1;
endwhile
while (<condition>) do
Action1;
...
ActionN;
endwhile
Example 1:
counter ¬ 1;
while (counter <= 100) do
print(counter);
counter ¬ counter + 1;
endwhile
7
Example 2:
s ¬ 0;
i ¬ 0;
while (i < 10) do
s ¬ s+1;
i ¬ i+1;
endwhile
repeat
Action1;
...
ActionN;
until (<condition>);
Example 1:
counter ¬ 1;
repeat
print(counter);
counter ¬ counter + 1;
until (counter > 100);
Example 2:
s ¬ 0;
i ¬ 0;
repeat
s ¬ s+1;
i ¬ i+1;
until (i = 10);
We can associate a group of actions that performs a given process with a name and a set of
parameters.
(See: The example of line in the drawing of stars)
We have two types of parameterized actions: Functions and procedures.