How the program ends in Pascal. Example programs in Pascal

How the program ends in Pascal. Example programs in Pascal

08.12.2023

The lesson examines the concept of “program structure in Pascal” and introduces the main compilers for work: turboPascal and pascal abc

On the Internet you will find a large amount of information on the Pascal language. The purpose of our portal is a consistent, logical presentation of brief theoretical information on the topic with the obligatory reinforcement of the material in the form of practical tasks based on solved examples. The tasks and lessons on Pascal presented on the site are built sequentially as the complexity increases, and ready-made solved examples will allow even a beginner to go through the material with ease. The site can be used as a visual aid for teachers and lecturers.

Pascal is a structured programming language. This means that to write a program, you must first create an algorithm for solving it for a computer. The syntax provides a certain structure of a program in Pascal:

  • [Program title]

label section (label) constant section (const) type section (type) variable section (var)

  • [Section of procedures and functions]

(section can be skipped if the program does not provide for the use of procedures or functions)

  • [Operators section]
1 2 3 begin statements; end.

begin statements; end.

The title with the service word program can be omitted from the program.

Variable is a quantity that has a name, type and value. The value of a variable can be changed while the program is running.

In the descriptions section, the most important part begins after the service word var (short for variable). This is where the types of variables in Pascal that will be described are indicated.

The section on labels, types and description of constants occurs, naturally, only if they are present in the program.

Operators section- the main part of the program, which always begins with the auxiliary word begin in Pascal (begin - translated from English as beginning). Accordingly, this section ends with the service word end (from English end).

Let's look at examples from the descriptions section:
Label section:

const a1 = 55; a2 = 3.14; ...

Type section:

var v11, v12,…: type1; v21, v22,…: type2; ...

Thus, the order of composing Pascal programs must correspond to the specified program structure.

It is known that programs cannot be immediately processed by the processor. First, they are translated into machine language using special translator programs. For the Pascal language (and a number of others), such a program is called a compiler (another type of translator is interpreters), of which there are quite a lot. But they all operate on approximately the same principle: you must first write an entire program from start to finish, then, if there are errors, correct them, and only then run the compiler, and the program will produce the result.

The first compilers were developed in 1992 by Borland International: two programming packages have been released - Borland Pascal 7.0 and Turbo Pascal 7.0.

Until recently, the most popular translator, used in all schools and universities, was the Turbo Pascal compiler or Turbo Pascal in Russian, which had an English interface and the standard window of which looked something like this:


On modern computers, some Turbo Pascal 7.0 modules (for example, the CRT module) do not work correctly. The service word uses connects one or another module to the program. Uses crt in Pascal is a module that allows you to display colored characters on a color screen when using text mode.

To clear a window in the Turbo Pascal compiler, use the ClrScr function (from English colorScreen - color the screen). The Turbo Pascal integrated environment is gradually being replaced by more modern compilers.

Today, the Russian-language compiler is gaining more and more popularity Pascal ABC and its new version Pascal ABC net , which can be downloaded from the official website. The development environment Pascal abc or, as it is often called, Pascal abs (and even so: abs and abc), stands out for its excellent reference material, syntactic highlighting and, as already mentioned, the Russian language. In addition, this is an entire platform (framework) that allows you to work in a visual editor with controls.


The crt module in pascal abc does not connect, and in general, working with modules is significantly simplified compared to Turbo pascal.

Thus, the Pascal abc programming environment compares favorably with all its predecessors.

You are in the Pascal programming materials section. Before we start programming, we need to clarify some concepts that we will need in the beginning. After all, you can’t just program like that. We cannot write the program in words - the computer does not understand anything other than zeros and ones. For this purpose, special symbolism was created in Pascal - the Pascal language, a set of reserved words that cannot be used anywhere else in your programs except for their intended purpose. Let's list the basic concepts that we will need at the beginning:

✎ 1) program – in English “program”, written at the very beginning of the code, followed by the name of the program in Latin and a semicolon. For example: program Summa; − a program called Summa. But this part of the code, called the program header, does not need to be written - it is present only for clarity and shows what problem this program solves. Here we used the word “code” - this is the name of the text entry of the program.

✎ 2) integer – in English means “integer” (or simply “integer”) and in Pascal is used to denote 32-bit (8 bytes) signed integers from the range [-2147483648, 2147483647] . We will look into what these large numbers mean later.

✎ 3) real – from English “real”, “real”, “real”, “real”. In the Pascal language, this term denotes real numbers in the range [-1.8∙10 308, 1.8∙10 308]. These are very large numbers, but 15 - 16 significant digits are displayed. By the way, the integer and real data types in the PascalABC.Net programming environment are always automatically highlighted in blue.

✎ 4) const – analogue of English. "constant", meaning "constant", "constant". In Pascal, this is a quantity that cannot change. It is written like this:


This entry must be taken as it is written: the number N is 12, S is 5, “pi” is 3.14 (as in mathematics, only a dot is used instead of a comma in Pascal). In the last line we used a double slash (two forward slashes), followed by text - this is how comments are written in Pascal, and the program does not perceive them. Everything that begins with a double slash and until the end of the line is a comment, which is written to explain the program and is always highlighted in a different color (in PascalABC.Net it is green; Turbo Pascal does not use this type of comment). There is another type of comment - this (text enclosed in curly brackets, just like here, also highlighted in green). This type of comment can be valid for several lines in a row - from the beginning of the bracket to its closing, and the compiler does not perceive everything that is in the middle of such a construction as code and simply skips it.

In reality the recording format const a little more complicated. According to the rules, we had to write:

1 2 3 4 const N: type integer;

Description:

")" onmouseout="toolTip()">integer
= 12 ; //number N – integer type S: type integer;

Description:
Represents a 32-bit signed integer.

Value range: -2 147 483 648 .. 2 147 483 647")" onmouseout="toolTip()">integer
= 5 ; //number S – integer type pi: type real;

Description:
Represents a double precision floating point number.

Size: 8 bytes
Number of significant figures: 15 - 16
Value range: -1.8∙10 308 .. 1.8∙10 308
")" onmouseout="toolTip()">real
= 3.14 ; //number "pi" - real

After declaring each value, its type is indicated, and then a value is assigned. But the previous entry is also correct, since the Pascal compiler is configured so that it automatically determines the type of a constant. But this cannot be said about the next type of numbers - variables.

✎ 5) var – comes from English. “variable” (“variable” or “changeable”), which in Pascal means a value that can change its value during the program. It is written like this:


As can be seen from the entry, there is no “=” sign here - variables of the same type are recalculated (separated by commas) and only the type is indicated after the colon. The variables N, m (integer) and Q, r, t (real) in the program can change values ​​within the limits of integer and real, respectively. One more note: the description of variables always comes after the description of constants (constants) - the const construction comes first, and then var.

✎ 6) begin – translated from English means “to begin” and Pascal means the beginning of the main program in which commands (operators) are written. After the word begin There is no semicolon.

✎ 7) end – in English. “end”, and in Pascal it means the same thing (end of the program). After the last word end there is always a period. We have emphasized the word “last” because the use of the construction begin–end perhaps in one more case: these are the so-called operator brackets, which are used to combine several operations under one operator. But more on that later. So the main program will look like this:

1 2 3 4 5 6 begin < оператор 1 > ; < оператор 2 > ; . . . . . . . < оператор N > ; end.

Here, the operators in the body of the program are different commands to the compiler.

✎ 8) write – in English means “to write”. This operator displays the text placed in it, which is why it is called the output operator. The text placed inside it is highlighted in blue and written like this:

Write( "this text is displayed on the screen");

A message inside parentheses and quotes will be shown in the console window (you can't just put it in parentheses without quotes). After executing this statement we will see on the screen:

this text is displayed on the screen

In this form, the write operator is used in the case when you need to show a hint, explanation, comment, etc. And if you also need to display a numerical value, say, S = 50 sq. m, then the format is used:

Write(, S);

As a result, we get the result on the screen:

The area is equal to: S = 50

And if you need to display units of measurement, you need to insert the text in quotes again after S:

Write( "The area is equal to: S = ", S, " sq.m" );

After executing the last output statement, we get the following output on the screen:

The size of the area is: S = 50 sq.m

✎ 9) writeln – the same as write, but after execution the cursor will be moved to the next line.

✎ 10) read – translated from English means “to read”, so read is called the read or data input operator. It is written as read(N), which means that the value N must be entered, where N is any number, or text, or other type of variable. For example, if we need to enter the age of a person who is 32 years old, we can write it like this:


In the first line of this code, the program displays the question “ What is your age?" and moves the cursor to the next line (ending ln); in the second line we print “Year =” (space at the beginning); Next we see the readln(Year) operator, which means it is necessary to enter the age Year (number 32); finally, we display the messages “My age”, “32” and “years. " one by one. You need to watch the spaces carefully. As a result of executing this code, we will receive the message:

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

✎ 11) readln – the same as read, only with a new line. Indeed, in the above example, after introducing the number Year, we only write in the next line: “ My age is 32 years old».

That's all for now. On the next page we will write the first program, and in Pascal programming this will be our

Professional development environment for creating programs and applications of any level of complexity. Combines the classic simplicity of Pascal with all the capabilities of the modern .NET development environment used by professional developers around the world. In addition, the Pascal programming language is taught in a school computer science course, giving students a basic knowledge of operators and variables. Thus, learning Pascal abs is better for beginners than learning other programming languages.

The course of seven practical video lessons is ideal for those who want to learn how to make a program in Pascal ABC, regardless of their skill level. Each lesson has its own topic, so you can watch them either in order or selectively to deepen and expand your knowledge in a particular area.

Pascal ABC Lessons

The Pascal ABC lessons presented in the video course are based on the development of application programs and provide practical knowledge. All the programs that you write during the video course are fully working and can be used in everyday life - there is no “water” or empty theory in the course.

We master the editor interface and write our first lines of code.


We study the logic of working with numbers and design a timer.


We study how a programming language compiles source code.



We use Pascal to find a solution to the problem about schoolgirl Anna.


We program a real virtual music synthesizer.


We master complex mathematical functions and create a full-fledged engineering calculator.



We create the “correct” phone book based on the database.


Lesson 1 - First program
Lesson 2 - Prime numbers
Lesson 3 - Compilers (Part 1)
Lesson 3 - Compilers (Part 2)
Lesson 4 - Solving a school problem
Lesson 5 - Making a Piano
Lesson 6 - Advanced Calculator (Part 1)
Lesson 6 - Advanced Calculator (Part 2)
Lesson 7 - Convenient phone book (Part 1)
Lesson 7 - Convenient phone book (Part 2)
Lesson 7 - Convenient phone book (Part 3)
Lesson 7 - Convenient phone book (Part 4)
Lesson 8 - Working with graphics. Particle System (Part 1)
Lesson 8 - Working with graphics. Particle System (Part 2)
Lesson 8 - Working with graphics. Particle System (Part 3)
Lesson 8 - Working with graphics. Particle System (Part 4)

So, on the previous page we defined the following concepts: program, integer, real, const, var, begin, end, write, writeln, read, readln. Let's write simple Pascal programs using them. But first, let's find out what parts a Pascal program consists of.

Any program in the Pascal programming language has three components: 1) header; 2) descriptions section; 3) the body of the program.

✎ 1) The title is the word Program, followed by the name of the program in Latin and a semicolon at the end. The name cannot begin with a number, must not coincide with reserved words (begin, end, integer, etc.) or variable names from the descriptions section (see below), and the use of any symbols (' @', '%', '&', etc.), except underscore. It is advisable to give a name with a meaning that would reflect the essence of the program itself. But you don't have to write a title.

✎ 2) Descriptions section – Since we will first consider the simplest tasks, the second section will contain either a description of constants or variables (see paragraphs 4 and 5 of the article Introduction to Pascal).

✎ 3) The body of the program is a block of statements in which commands to be executed are written. This block begins with the word begin(“beginning”), and ends with the word end.(“end”) with a period at the end (paragraphs 6, 7 of the same article).

Thus, in general, a Pascal program looks like this:


When solving the simplest problems, we will first limit ourselves to integers. Before we start programming, we will indicate the simplest 4 operations on numbers in Pascal: “+” - addition, “-” - subtraction, “*” - multiplication, “/” - division. As you can see, in programming and mathematics, elementary operations on numbers are denoted in the same way. Let's solve the following problem:

Task. Let integers A and B be given. Find their sum.


Let's call the program for finding the sum Summa. Then the part of the code responsible for the name of the program will look like:

1 Program Summa; (the program is called Summa)

Let us denote the sum of numbers A and B by S. Since the numbers are integers, the sum will be an integer. Thus, in describing the numbers A, B, S we will use a variable of integer type. But how to describe these numbers in the descriptions section - as constants ( const) or as variables ( var)? It all depends on the conditions of the problem. We can write the numbers A and B both as constants and as variables. But in any case, the sum S will be of variable type, since we do not know its value.

So, let the values ​​of the numbers A and B be given, equal, for example, to 23 and 76, respectively. Since we know the values ​​in advance, A and B will be like const, and S will be described as var. Considering that the description of constants comes before the description of variables, the description section in this case will be as follows:


Thus, the body of the program will consist of calculating the sum S and displaying it on the screen. In Pascal, to calculate the sum of A and B, you cannot simply write S = A + B; such a notation is possible if you need to check whether the number S is equal to the sum A + B. A in order to write the value of the sum A + B into the variable S, you need to “assign” the value of this sum to the variable S, i.e. write the sum A + B to the place in the computer memory that is allocated for the variable S. This is like “shoving” the value of one number into the value of another. The assignment is written like this:

To output the result, use the write output operator with a comment on the screen:

write("The sum of numbers 23 and 76 is: S = ", S);

Combining our 1) program header, 2) description section and 3) program body, we get a small code that can be copied into PascalABC.Net or another version of Pascal:


After executing this program we will see on the screen:

The sum of numbers 23 and 76 is: S = 99

Everything is fine and the program works. But here we wrote something unnecessary. Look carefully at the write output statement at the end - the sum S is displayed there. Will anything change if instead of S we write the value of the sum itself, that is, A + B? That's right, nothing. Only then we need to remove the variable S from our code, and instead use the sum A + B when displaying it on the screen. Here is the simplified code:


In this case, the result on the screen will be like this:

The sum of numbers 23 and 76 is 99

This is our simplest first program. You can see other problems of this type on the page Rectangle, circle and parallelepiped. Calculation of areas, perimeters and volumes. True, they use variables of the real type real, but the essence is the same: solve the problem without an assignment operator. In general, this operator is used where it is necessary to calculate an intermediate variable that is used in calculating the main result. Therefore, where you can do without assignment when calculating the result (the answer to a problem), you must write the expression directly into the write output operator. Only if the expression is too large and does not fit into the output statement, it must be evaluated separately by introducing additional variables (var). But in the simplest cases, as a rule, you can do without the assignment operator.


On the next page we'll talk about percentages. In the meantime, if you are a fan of World of Warcraft, then this will help you

Let's say you downloaded and installed Pascal. Then open it. To program you need a little knowledge of English. The essence of our program is that it can add any numbers. For this we need variables. We will have 2 numbers. But in every problem, even the simplest one, there is an answer. So we add 3 variables.

Step 2

So. To format our variables, go to English and write the word “var” at the beginning of the program. And then, separated by a comma, our 3 variables, a, b, c, and separated by a colon, put the type “integer.” Then write begin - to begin.
At this stage the program will look like this:

var a,b,c:integer;
begin

Step 3

Well, then the program itself! We will make sure that when we start working, our program will ask you to enter the value of our variables. To do this, write the English word “read” and in parentheses indicate which variables need to be read.
read(a,b);
Then, as in mathematics, write the formula. Just be sure to have a colon before equals! c:=a+b;
Then we need the program to output the response: write©;
OK it's all over Now! we finish the program and write end.
Our program looks like this:
var a,b,c:integer;
begin
read(a,b);
c:=a+b;
write©;
end.
By the way, you can copy this program into Pascal and check it or write it yourself.
After launching, the program will ask for input, enter numbers separated by a space!

© 2023 hecc.ru - Computer technology news