Algorithms and Pascal Programming Basics
Algorithms and Pascal Programming Basics
I.1. Definition
1) Algorithmic:
2) Algorithm:
3) Program:
4) Programming language:
It is a language that allows for the formulation of algorithms and the production of programs.
computers that apply these algorithms.
5) Compiler:
The name of the Pascal Algorithm/Program, the variables/constants used are declared
in the header while the body part contains the various instructions.
Algorithm Algorithm_name
Const // List of constants // Header of the Algorithm
Var // variables to be used// :Type
Debut
Body of the Algorithm
⋮ List of instructions
End.
Program Nom_du_programme;
Uses units to be used
Const // constants to be used // Program Header
Var // variables to be used // :Type ;
Begin
⋮ // List of instructions //
Body of the Program
End.
A constant is a variable that does not undergo any change over the course of a
program.
1) Type Numérique :
Numeric type Beach
Octet, 8 bits (Byte) 0 to 255
Simple integer, 16 bits (Integer) -32768 à 32767
Long integer, 32 bits (LongInt) -2,147,483,648 to 2,147,483,647
-3.40×1038-1.40×10-45 for negative values
Real simple
1.40 times 10-453.40 x 1038 for positive values
-1.79×10308-4.94 × 10-324 for negative values
Real double (Double)
4,94×10-324a1.79×10308 for positive values
Some languages allow other numeric types, including:
• the monetary type (with strictly two digits after the decimal point)
• enter the date (day/month/year).
2) Alphanumeric Type:
3) Boolean Type:
The last type of variables is the boolean type: it only stores values.
TRUE and FALSE (True, False).
There are other functions for numerical calculations presented in the following table:
Function Algorithm Pascal Language
Absolute value of 'x' Abs(x) Abs(x)
integer part of 'x' No need Trunc(x)
The rounding of 'x' No need Round(x)
Cosine of 'x' (in radians): cos(x) cos(x)
Sine of "x" (in radians): sin(x) sin(x)
Tangent of 'x' (in radians): tan(x) tan(x)
Exponential of 'x' exp(x) exp(x)
Natural logarithm of 'x' Ln(x) Ln(x)
Square root of 'x' Sqrt (x) Sqrt (x)
If ofn n= 2: "sqr(x)"
Power "n" of the number "x" Pow (x,n) means x raised to the power
If not: "exp(n*ln(x))"
The mathematical "π" is represented by Pique, whether for the Algorithm or in Pascal.
Example:
A ← "Mohamed"
B← ʺHello ʺ & A
The value of B is: Hello Mohamed.
4) Relational operators:
Operator Operation
= equal
<> Different
> Strictly greater
< Strictly less than
>= Greater than or equal to
5) Logical operators:
Operator Operation Types opérandes Type résultat
Not Logical Negation Boolean Boolean
And And Logic Boolean Boolean
Or Or Logic Boolean Boolean
Xor Exclusive Boolean Boolean
1) Read:
2) Write:
Example :
Hello Algorithm
Var A : string
Debut
Write (ʺenter your nameʺ)
Read (A)
Write ("Hello", A)
End.
In an algorithm, the conditional structure begins with "if" and ends with
"Finish". In a Pascal program, if the conditional structure contains several
instructions must be enclosed by 'Begin' and 'end ;'
second conditional
siconditionalors If condition then
Instruction_1 If condition then
otherwise Instruction_1
Instruction_2 Otherwise
Finish Instruction_2
otherwise Else
Instruction_3 Instruction 3;
Finish
Note:
Example:
If the student's average 'moy' is less than 10, they are 'failed'; if it is between 10 and
In 15, there will be the mention "[Link]" if the average exceeds 15, the mention is "very good".
These are iterative structures in which one must repeat a process until
a predefined condition, we will study three types of loops: 'While', 'Repeat'
and 'For'.
Example:
Double a number "a" entered by the user and display it; as long as it is
less than 1000.
Algorithm Pascal Language
while a < 1000 do
As long as < 1000 do begin
to write"The double of", a, "is: ", a*2 writeln('The double of ', a, ' is ', a*2);
a← a*2 a:=a*2;
As long as End;
The instruction "Repeat" is used in cases where the program must perform
operations until a condition is met.
The instructions between 'Repeat' and 'Until' are executed in sequence until
that at the end of the sequence, the boolean condition "condition" is true. So no need to
group instructions in a block (Begin...end;).
We use the 'For' loop when we know exactly how many iterations we need.
achieve.
Note:
• In the absence of "ValeurDuPas"; the algorithm uses the default value "1".
• The command 'To' increments while 'DownTo' decrements by '1'.
value of the meter.
Example:
Multiply the number "a" by the numbers 0 to 10 and display the results.
Example:
varVect:array[1..5]ofInteger;
Reading or writing the terms of the table is done via a loop, for our example:
for i = 1 to 5 do read(Vect[i]);
One can create arrays with components of all types, including arrays.
And consequently, we can create multi-dimensional arrays for our course.
we will limit ourselves to two-dimensional arrays.
for example; we can see a matrix as a table of lines that are themselves
component tables.
varMat:array[1..3]of array[1..4]ofInteger;
you
Access to the terms of the matrix is done through these indices (Mat[i,j]):
( ( ) ) 1.2
1.1 ( ) 1.3 ( )1.4
= ( (2.1
) ( ) ( ) 2.2
) 2.3 2.4
( 3.1) ( )
3.2 3.3 ( )3.4 ( )
for reading or displaying the values of the terms of the matrix, it is preferable
to use two loops:
for i = 1 to 3 do
begin
for j:=1 to 4 do write(Mat[i,j], ' ');
writeln;
end;