PASCAL
The basic structure:
The basic structure of a program written in pascal is composed of 5 main points:
1. Header;
2. Riga declaring variables;
3. A statement that declares the start of the program;
4. Body of the program;
5. Education declaring the end of the program.
Each line of a program written in Pascal must necessarily end with a semicolon (;), this rule does not apply to lines containing specific instructions outlined later in this guide. All the text of the program, also called the source code can be written indifferently in either upper or lower case.
EXAMPLE OF BASIC STRUCTURE:
Program program name, header
var VARIABLE: TIPOVARIABILI; line that declares the variables
begin instruction declares the start of the program
...
Body of the program ...
...
readln; allows the program to continue to run at the end of the program until a key is pressed
end. statement declaring the end of the program
"Readln" before END (mandatory in newer compilers), serves to lock the screen at the end of the program:
practically the compiler expects it to be pressed a button to jump to the next, that is, END, and then terminate the execution of the program.
Without readLn the program runs the same, but the compiler does not allow stopping at the end of the correct display of execution.
1. Header;
2. Riga declaring variables;
3. A statement that declares the start of the program;
4. Body of the program;
5. Education declaring the end of the program.
Each line of a program written in Pascal must necessarily end with a semicolon (;), this rule does not apply to lines containing specific instructions outlined later in this guide. All the text of the program, also called the source code can be written indifferently in either upper or lower case.
EXAMPLE OF BASIC STRUCTURE:
Program program name, header
var VARIABLE: TIPOVARIABILI; line that declares the variables
begin instruction declares the start of the program
...
Body of the program ...
...
readln; allows the program to continue to run at the end of the program until a key is pressed
end. statement declaring the end of the program
"Readln" before END (mandatory in newer compilers), serves to lock the screen at the end of the program:
practically the compiler expects it to be pressed a button to jump to the next, that is, END, and then terminate the execution of the program.
Without readLn the program runs the same, but the compiler does not allow stopping at the end of the correct display of execution.
The Header:
The header of a program written in Pascal consists of the word PROGRAM followed by a space and the name of the program. The program name is not read by the compiler, so it can be any name.
The name of the program:
MUST: be written without spaces;
MUST NOT: to be a "reserved word" contain accented letters contain punctuation marks and symbols.
If you want to include spaces in the name of the program you can use dashes (_).
THE WORDS "CONFIDENTIAL" NOT TO BE USED:
Absolute
And
Array
Asm
Begin
Houses
Const
Destructor
Div
Do
Downto
Else
End
File
For
Forward
Funtion
Goto
If
Implementation
In
Interface
Label
Mod
Nil
Not
Object
Of
Or
Packed
Private
Procedures
Program
Record
Repeat
September
Shr
String
Then
To
Type
Until
Uses
Var
While
Whit
Xor
The name of the program:
MUST: be written without spaces;
MUST NOT: to be a "reserved word" contain accented letters contain punctuation marks and symbols.
If you want to include spaces in the name of the program you can use dashes (_).
THE WORDS "CONFIDENTIAL" NOT TO BE USED:
Absolute
And
Array
Asm
Begin
Houses
Const
Destructor
Div
Do
Downto
Else
End
File
For
Forward
Funtion
Goto
If
Implementation
In
Interface
Label
Mod
Nil
Not
Object
Of
Or
Packed
Private
Procedures
Program
Record
Repeat
September
Shr
String
Then
To
Type
Until
Uses
Var
While
Whit
Xor
The Variables:
A variable is a container within which there are data that can vary during the execution of the program. The variables in a program written in Pascal are declared in a specific line after the word VAR through the names. The latter, also called identifiers are assigned freely, but should NOT:
1. Contain spaces;
2. Be equal to the name of the program;
3. Contain punctuation marks or symbols;
4. Be the symbols or numbers;
5. Being a reserved word;
6. Contain accented letters.
If there are more variables, the various identifiers are separated by commas (,).
The details of these variables can be of different types: text, integers, real numbers, etc.. The type of a variable must be indicated after
identifier, followed by the latter in the colon (:) and writing the variable's type.
If there are more variables of the same type (eg 3 numbers, strings, etc. 2.), Will indicate the various identifiers separated by commas, they will follow
last identifier of a colon (:) and write the type of the variable.
Example (a variable declaration):
var identifier: Type;
Example (declaration of several variables):
var identifier, ..., ..., identifier: type;
When variables are many and different types of syntax is as follows:
var identifier, ..., ..., identifier: type1;
var identifier, ..., ..., identifier: type2;
The main types of variables are:
- Integer: The variable is an integer in the range -32768 to +32767
- Byte: The variable is an integer between 0 and 255
- Word: The variable is an integer between 0 and 65535
- Shortint: The variable is an integer between -128 and +127
- Longint: The variable is an integer between -2147483648 and +2147483648
- Real: The variable is a real number between 2.9E-39 and 1.7E38
- String: The variable is of type text
- Char: The variable is a single character of the ASCII table
- Boolean: The variable can have the values TRUE or FALSE
1. Contain spaces;
2. Be equal to the name of the program;
3. Contain punctuation marks or symbols;
4. Be the symbols or numbers;
5. Being a reserved word;
6. Contain accented letters.
If there are more variables, the various identifiers are separated by commas (,).
The details of these variables can be of different types: text, integers, real numbers, etc.. The type of a variable must be indicated after
identifier, followed by the latter in the colon (:) and writing the variable's type.
If there are more variables of the same type (eg 3 numbers, strings, etc. 2.), Will indicate the various identifiers separated by commas, they will follow
last identifier of a colon (:) and write the type of the variable.
Example (a variable declaration):
var identifier: Type;
Example (declaration of several variables):
var identifier, ..., ..., identifier: type;
When variables are many and different types of syntax is as follows:
var identifier, ..., ..., identifier: type1;
var identifier, ..., ..., identifier: type2;
The main types of variables are:
- Integer: The variable is an integer in the range -32768 to +32767
- Byte: The variable is an integer between 0 and 255
- Word: The variable is an integer between 0 and 65535
- Shortint: The variable is an integer between -128 and +127
- Longint: The variable is an integer between -2147483648 and +2147483648
- Real: The variable is a real number between 2.9E-39 and 1.7E38
- String: The variable is of type text
- Char: The variable is a single character of the ASCII table
- Boolean: The variable can have the values TRUE or FALSE
Control structures:
An algorithm is a finite set of instructions that are used to specify a series of transactions by which it is possible to solve a problem. A program
in Pascal then, as in any other programming language, is an algorithm. According to the theorem of Böhm-Jacopini (1966): "Any algorithm can be expressed using only the structures of sequence, selection and iteration." These 3 models are called control structures.
The structure of the programs examined so far is the sequence, that is, the statements are executed one after the other:
Program example;
Begin
...
Instruction1;
Statement2;
Statement3;
...
readln;
End
The selection structure, or alternatively, in Pascal is represented according to the scheme:
IF (condition) THEN
instruction1
ELSE statement2;
If the condition is true instruction1 is executed, otherwise (ie, if the condition is false) is executed statement2.
If the instructions are more than one using the normally begin at the beginning and end with a semicolon at the end.
This type of instruction is executed only once, does not work as a loop (iteration)!
The structure of iteration, or repetition, consists of a loop with a condition to the end or beginning.
If the cycle has the condition at the end of the coding in Pascal is:
REPEAT
...
instructions;
...
UNTIL (condition);
The instructions, or education, including Repeat Until and will be repeated until the condition becomes true, then the structure is repeated for fake!
If the cycle has the condition at the start coding in Pascal is:
WHILE (condition) DO
BEGIN
...
instructions;
...
END;
The statements between Begin and End will be repeated until the condition becomes false, then the structure is repeated for real!
If there is only one statement to be repeated, the BEGIN and END can be omitted.
There is a structure of repetition without a real condition, called counter with repetition or enumerative.
Its coding in Pascal is:
FOR counter: = initialvalue finalvalue TO DO
BEGIN
...
instructions;
...
END;
The statements between Begin and End will be repeated as many times as needed to bring the value of the counter from the initial value to the final value.
(Example: for C: = 1 TO 15 DO, to bring the counter C by 1 to 15 need to 15 steps, then the operations will be performed 15 times).
If there is only one statement to be repeated, the BEGIN and END can be omitted as the WHILE loop seen before!
in Pascal then, as in any other programming language, is an algorithm. According to the theorem of Böhm-Jacopini (1966): "Any algorithm can be expressed using only the structures of sequence, selection and iteration." These 3 models are called control structures.
The structure of the programs examined so far is the sequence, that is, the statements are executed one after the other:
Program example;
Begin
...
Instruction1;
Statement2;
Statement3;
...
readln;
End
The selection structure, or alternatively, in Pascal is represented according to the scheme:
IF (condition) THEN
instruction1
ELSE statement2;
If the condition is true instruction1 is executed, otherwise (ie, if the condition is false) is executed statement2.
If the instructions are more than one using the normally begin at the beginning and end with a semicolon at the end.
This type of instruction is executed only once, does not work as a loop (iteration)!
The structure of iteration, or repetition, consists of a loop with a condition to the end or beginning.
If the cycle has the condition at the end of the coding in Pascal is:
REPEAT
...
instructions;
...
UNTIL (condition);
The instructions, or education, including Repeat Until and will be repeated until the condition becomes true, then the structure is repeated for fake!
If the cycle has the condition at the start coding in Pascal is:
WHILE (condition) DO
BEGIN
...
instructions;
...
END;
The statements between Begin and End will be repeated until the condition becomes false, then the structure is repeated for real!
If there is only one statement to be repeated, the BEGIN and END can be omitted.
There is a structure of repetition without a real condition, called counter with repetition or enumerative.
Its coding in Pascal is:
FOR counter: = initialvalue finalvalue TO DO
BEGIN
...
instructions;
...
END;
The statements between Begin and End will be repeated as many times as needed to bring the value of the counter from the initial value to the final value.
(Example: for C: = 1 TO 15 DO, to bring the counter C by 1 to 15 need to 15 steps, then the operations will be performed 15 times).
If there is only one statement to be repeated, the BEGIN and END can be omitted as the WHILE loop seen before!
The vectors (array):
The array (vector from the English), it is nothing more than a memory which stores more variables. The difference compared to the normal variables
is that you can use hundreds of memory allocations, without, however, declare an equally large number of variables. In fact, with the same name indicates a list of elements, each of which is individually accessible. In this case, you want to store 40 (or less) names, but it would not be convenient to write:
var name1, name2, name3, nome4, nome5, ..., nome40: string;
The array enables us to this! So, to declare an array, you use the building:
<variable name>: array [min .. max value] of <type of associated
To use, read or write an element of an array you just have to write down its name dallindicazione the Index that distinguishes it, enclosed in square brackets, like this:
var numbers: array [1 .. 50] of integer;
...
read (number [9]);
...
write (number [98]);
...
number [7]: = number [2] * 3;
is that you can use hundreds of memory allocations, without, however, declare an equally large number of variables. In fact, with the same name indicates a list of elements, each of which is individually accessible. In this case, you want to store 40 (or less) names, but it would not be convenient to write:
var name1, name2, name3, nome4, nome5, ..., nome40: string;
The array enables us to this! So, to declare an array, you use the building:
<variable name>: array [min .. max value] of <type of associated
To use, read or write an element of an array you just have to write down its name dallindicazione the Index that distinguishes it, enclosed in square brackets, like this:
var numbers: array [1 .. 50] of integer;
...
read (number [9]);
...
write (number [98]);
...
number [7]: = number [2] * 3;
The ordering of a vector:
Sorts the elements in an array means to put them in alphabetical order or if strings are in order of magnitude if they are numbers.
There are many sorting algorithms.
We will analyze only one, the most simple: the sequential method.
SEQUENTIAL METHOD:
This algorithm is very effective with a number of elements not excessive.
The sorting method presented consists in a comparison of each element with all the other elements that are arranged in successive positions as those examined, starting from the first element until the last.
All the times that between subsequent elements meets a smallest element, is exchanged between the two so that, at the end of the comparison of each element with the next examination is obtained in the position in the minimum value than the next.
At the end of the items will be arranged from smallest to largest, in ascending order.
In the example that we will see the sort is ascending, if you wanted to get a descending sort enough to change the direction of the comparison.
Program Ordinamento_ Elementi_Array;
var temp, z, i, j: integer;
a: array [1 .. 5] of integer;
begin
writeln ('Sort items of an array');
for z: = 1 to 5 do
begin
writeln ('Enter the component', z, ''' s array');
write ('part number', z, '='); readln (a [z]);
end;
writeln;
for i: = 1 to 5 do
begin
for j: = i +1 to 5 do
begin
if a [i]> a [j] then
begin
temp: = a [i];
a [i]: = a [j];
a [j]: = temp;
end;
end;
end;
for z: = 1 to 5 do
writeln ('part number', z, '=', a [z]);
readln;
end.
There are many sorting algorithms.
We will analyze only one, the most simple: the sequential method.
SEQUENTIAL METHOD:
This algorithm is very effective with a number of elements not excessive.
The sorting method presented consists in a comparison of each element with all the other elements that are arranged in successive positions as those examined, starting from the first element until the last.
All the times that between subsequent elements meets a smallest element, is exchanged between the two so that, at the end of the comparison of each element with the next examination is obtained in the position in the minimum value than the next.
At the end of the items will be arranged from smallest to largest, in ascending order.
In the example that we will see the sort is ascending, if you wanted to get a descending sort enough to change the direction of the comparison.
Program Ordinamento_ Elementi_Array;
var temp, z, i, j: integer;
a: array [1 .. 5] of integer;
begin
writeln ('Sort items of an array');
for z: = 1 to 5 do
begin
writeln ('Enter the component', z, ''' s array');
write ('part number', z, '='); readln (a [z]);
end;
writeln;
for i: = 1 to 5 do
begin
for j: = i +1 to 5 do
begin
if a [i]> a [j] then
begin
temp: = a [i];
a [i]: = a [j];
a [j]: = temp;
end;
end;
end;
for z: = 1 to 5 do
writeln ('part number', z, '=', a [z]);
readln;
end.
Multidimensional arrays or "Matrix"
If a one-dimensional array is identified by a single index, a multidimensional array support many more:
<name>: array [min .. max, from .. to, a .. b, ...] of <type>
And to keep in mind, however, that the maximum space in memory, intended to variables, a program that can occupy is 64KB, so it is always advisable to reduce to a minimum the use of particular types of variables (such as the extended, the more large).
A small example to better understand the functioning of this particular type of array, this program reads the data in an array and writes them to output in the form of "multiplication table" as we used to see in arithmetic:
program MATRIX;
var n, m, ir, ic: integer;
var mat: array [1 .. 100,1 .. 100] of integer;
begin
write ('Enter how many lines you want to consider?');
readln (n);
while (n <= 0) do
begin
write ('Please enter a number POSITIVE!');
readln (n);
end;
write ('How many columns you want to consider?');
readln (m);
while (m <= 0) do
begin
write ('Please enter a number POSITIVE!');
readln (m);
end;
ir: = 1;
ic: = 1;
while (ir <= n) do
begin
writeln ('Data Entry line', ir);
ic: = 1;
while (ic <= m) do
begin
write ('Enter the data of', ic, 'column');
readln (mat [ir, ic]);
ic: ic = +1;
end;
ir: ir = +1;
end;
ic: = 1;
ir: = 1;
while (ir <= n) do
begin
ic: = 1;
while (ic <= m) do
begin
write (mat [ir, ic], '');
ic: ic = +1;
end;
writeln;
ir: ir = +1;
end;
readln;
end.
<name>: array [min .. max, from .. to, a .. b, ...] of <type>
And to keep in mind, however, that the maximum space in memory, intended to variables, a program that can occupy is 64KB, so it is always advisable to reduce to a minimum the use of particular types of variables (such as the extended, the more large).
A small example to better understand the functioning of this particular type of array, this program reads the data in an array and writes them to output in the form of "multiplication table" as we used to see in arithmetic:
program MATRIX;
var n, m, ir, ic: integer;
var mat: array [1 .. 100,1 .. 100] of integer;
begin
write ('Enter how many lines you want to consider?');
readln (n);
while (n <= 0) do
begin
write ('Please enter a number POSITIVE!');
readln (n);
end;
write ('How many columns you want to consider?');
readln (m);
while (m <= 0) do
begin
write ('Please enter a number POSITIVE!');
readln (m);
end;
ir: = 1;
ic: = 1;
while (ir <= n) do
begin
writeln ('Data Entry line', ir);
ic: = 1;
while (ic <= m) do
begin
write ('Enter the data of', ic, 'column');
readln (mat [ir, ic]);
ic: ic = +1;
end;
ir: ir = +1;
end;
ic: = 1;
ir: = 1;
while (ir <= n) do
begin
ic: = 1;
while (ic <= m) do
begin
write (mat [ir, ic], '');
ic: ic = +1;
end;
writeln;
ir: ir = +1;
end;
readln;
end.