Last Updated:

Pascal: Designed to write algorithms

You are in the subsection with information on programming with the Pascal language. Before proceeding directly to programming, it is necessary to dwell on the explanation of some points that will be useful to us. After all, without knowing the initial information, it is impossible to move on.

We do not have the opportunity to write a program in words, since the computer "sees" all the information in the form of 0 and 1. To do this, the Pascal programming language contains a set of reserved words that can only be used for their intended purpose.

turbo pascal

General information about the Pascal programming language

Let's first define what a programming language is. First of all, these are formal languages designed to write algorithms. And they will be performed, of course, by a computer.

The Pascal language, which we will study, owes its name to the French scientist Blaise Pascal. The language was developed in the 70s of the last century.

This language is considered to be universal, since with its help you can solve a huge number of different problems.

Commands and operands used in Pascal

program (translated as "program") is written in the top line of the program. Then in the same line the name of the future program is written, and the characters ";" are prescribed. For example: program Summa; - The name indicates that the solution of the algorithms will be aimed at finding the sum of numbers. This part of the code is optional. Usually it is used for clarity, to understand what goal the program should achieve.

Importantly! A "code" is a text entry for a program.

integer (translated from English as "integer") - this data type is used in Pascal to define sign integers from the range [-2147483648, 2147483647]. The size of these numbers is 32 bits or 8 bytes. We will talk more about these numbers a little later.

real (translated from English as "real") - this type of data marks the real numbers from the scope [-1.8∙10 to the 308th degree, 1.8∙10 to the 308th power]. Despite the fact that these are quite large values, fifteen to sixteen significant digits are displayed.

Importantly! Both of the data types discussed in the code, when spelled correctly, light up in blue.

const (translated from English as "constant", "constant") - in Pascal this value remains unchanged. It looks like this:

const N = 12;
pi = 3.14; //Pi"

This recording format must be interpreted as it is written, that is: the value of N = 12, S = 5, the number "Pi" = 3.14.

Importantly! In mathematical language, a comma is used to write the number "Pi", and for Pascal you need to prescribe a period.

In the last line, we spelled out two oblique dashes (slash), and after it the text is written. This is how comments are written in Pascal. So, everything that is written after two oblique lines and before the end of the line are comments that the program does not read when solving the problem. They stand out in green. This type of explanatory writing is only suitable for Pascal ABC.

For comments in Turbo Pascal, the text must be enclosed in curly braces. For example:{text placed in curly brackets, as well as here, also lights up in green}. This type of explanation can be active until the parentheses are closed.

In reality, the constant record looks somewhat more complicated:

const N: integer = 12; //number N - integer type
S: integer = 5; //number S - integer type
pi: real = 3.14; //number "pi" - real

After you have specified the name of each variable, you must specify its data type and only then you can specify a non-value. But all of the above does not negate the correctness of the previous entry const.

var (translated from English as "variable") is a variable whose value can change during the solution of the problem. It looks like this:

var N, m: integer;
Q, r, t: real;

Note that there is no "=" character in this entry. First, variables belonging to the same data type are listed separated by commas, the ":" sign is put and the type itself is indicated. The values N, m-integer, Q, r, t-weight. During program execution, variables can change the value within the types of values discussed.

Importantly! Variables are described after constants are labeled.

begin (translated from English as "to begin"). Based on the translation, we can conclude that in Pascal this command talks about the beginning of the main block of the program. It records all the necessary to obtain the result of the operation. After begin, the period ";" is not spelled out.

end (translated as "end"). In Pascal, this command also means the end of the program. After the last command "end", a period is prescribed.

Importantly! In Pascal, the begin and end commands can be used repeatedly. It depends on how many actions you want to perform within the task.

The main block of code will look like this:

begin <statement 1>;
<operator 2>; . . . . . . .
<N operator>;

Operators in this case are different commands of program code.

write (translated into Russian as "to write"). With this command, the user has the ability to display the text embedded in it on the screen. Such text is highlighted in blue. For example: write('this text is displayed on the screen');


Text in parentheses and quotation marks will be displayed in a specially designated window. After performing this action, we will see:

this text is displayed on the screen

There is another option for using the write command. It is used to display a numeric value on the screen, for example, S = 50 sq.m., then we get:

write(‘Величина площади равна: S = ‘, S);

The output window will display:

The size of the area is: S = 50

To display the units of measurement together with the value of the variable S, we get:

write('Dimension squares equal: S = ', S, ' sq.m');

Accordingly, the output window will be:

Dimension squares equal: S = 50 sq.m

writeln - similar to the previous command, will display data on the screen. But there is still an insignificant difference. After writeln, the cursor automatically switches to the next line.

read (translated as "to read"). Therefore, this operation means reading or entering data. It looks like this: read (N)-you need to enter the value of N, where N can take any value. For example, we need to write down a person's age. Let's suggest that he is 32 years old, then here's what happens:

year: integer;
writeln('What is your age?');
write(' Year = ');
{ Enter an integer number of years }
write('My age', Year, 'years');
{ Output result here }

In the top line there is the question "What is your age?"; next, we derive "Year=". You are then prompted to enter an age figure using the readln statement. In the alternate sequence, we display the data "My age", "32", "years". Ready! In the output window we get:

What is your age?
Year = 32
My age is 32 years

readln — has the same meaning as read, with the difference of translation to a new line.

These are only the first steps towards learning the Pascal programming language. In the next article, we will work with our first program.