It would seem that a program in Pascal is an easy and typical business for any programmer, but it cannot be said that the language itself is too weak for the modern world. It is not in vain that it is considered basic and is studied at school.
Often, to write programs in Pascal use a programming environment such as Turbo Pascal (Turbo Pascal).
Why should beginners choose this particular language? Let's try to figure it out. In order to fully master any programming language, you must have the appropriate literature. That is why most languages disappear immediately, in particular the Logo. It is not widely distributed in Russian-speaking countries, so there are no training books on it. At the moment, the most famous are Pascal, Java, Basic and C. Let's consider each separately. Java is mainly used for programming on the network. C is one of the most common languages, but also the most difficult (especially for beginners). In order to eventually learn how to write programs on it, it is best to first learn Pascal or Basic. They are both studied in schools (teacher's choice). The reason for their prevalence lies in the fact that in these languages you can write the same program as in C or C ++, but its development is much easier due to the simplicity of construction.
It is worth remembering that there are a sufficient number of versions of Pascal and Basic, but in schools, species such as Turbo Pascal 7.0 and QBasic are more common. If we take into comparison just them, then the latter is more designed to write small programs up to 50 lines long. Turbo Pascal is more powerful and faster.
First program in Pascal
An example of programs in Pascal can easily be found on the Internet, it is more difficult to understand how they work.
In order to understand how this language works, you need to write a small “task”. Suppose you want to create a program that will add two numbers: Q + W = E. What should be done to create it?
The letter E will be a variable (in mathematics - X). Be sure to give her a name (no more than 250 characters). It may consist of:
- letters of the Latin alphabet (A..Z);
- digits from 0 to 9. But it should be remembered that the name should not begin with a digit;
- character "_".
The title should not include:
- Russian letters;
- punctuation marks;
- specialist. characters such as the pound sign "#", the percentage "%", the dollar "$", the tilde "~", etc.
An example of a valid name might be red_velvet4, exo, or shi_nee. It must also be remembered that Pascal is case insensitive, so variables with the names "btob" and "BtoB" are treated as one. How exactly in the programming environment the "cells" are called and the cells are called will be understood below. After this process, you need to determine the type of variable. This is necessary so that the program correctly displays the result on the screen or prints it on paper. For example, if two numbers are added, then the numeric type is indicated, and if a greeting is displayed, then a string. For the usual addition of two numbers, the Integer type ("number") is suitable.
With the type for variables, everything is clear, but it must be assigned for all numeric variables in order to freely manipulate them.
Based on what is written above, you can easily write a small "task". The program in Pascal will look like this:
Program shi_nee;
Var Q, W, E: integer;
BEGIN
E: = Q + W;
END.
The first line, i.e. program shi_nee, is the name or title of the program itself. This is an optional component that does not affect the operation of the written calculation. If used, it must be the first one, otherwise the programming environment will throw an error.
Variables are described in the second line thanks to the “var” operator. It is necessary to list all the variables that appear in the program and add a type (integer) through the colon .
A pair of operators "BEGIN" - "END" begins and, accordingly, completes the program. They are the most important; between them are all the actions described by the programmer.
Language syntax
It is important to remember that each line of the program ends with a semicolon. An exception to the rule will be utility commands such as var, begin, const, etc. After the end operator end, a period must be set. In some cases, when the program has several attachments and operator brackets “BEGIN” - “END” are used, a semicolon can be put after this command.
In order to assign a cell its value, for example, E = 15, it is necessary to put a colon before the equal sign:
Q: = 15:
W: = 20;
E: = Q + W;
The colon in this language is called assignment. A Pascal program is written very easily if you master the syntax rules described above.
Testing
Running Turbo Pascal, you need to print the program, correctly positioning the required characters and setting the desired line order. The program can be launched through the context menu or using the keys Ctrl + F9. If everything is typed correctly, the screen will blink a little. In the event that an error is present, the program will pause, and the cursor will be positioned in the line where it exists. In this case, a description of the error itself will appear in the “Output” line, for example, error 85: “;” expexted. This line indicates that somewhere there is no semicolon. However, while paying attention to such problems is not worth it, you need to understand the structure using examples of programs in Pascal.
What should be studied first?
To begin with, fully understand each line of the program. Then pay attention to the syntax, put aside special operators in your memory, after which the punctuation mark is not put, learn the logical chain from begin and end. Remember exactly how the variables are set up, what type they need and why it is needed at all. Understanding the functioning of the programming environment itself will not be amiss. To do this, you can use the manual or "poke" in Turbo Pascal yourself. Constantly you need to practice, disassemble ready-made “tasks” from the Internet and slowly type in example programs on Pascal yourself.
One-dimensional arrays
The array is used to conveniently work with the same type of data, which in this case is located in consecutive memory cells, and not separately. It is unlikely that programmers are comfortable working with 50 or 100 variables. It is more convenient to write them to an array.
Elements that are in the group have their own number. In various programming languages, the score starts with a certain number, not necessarily with 1. But the example of programs in Pascal suggests that in it the numbering starts with it. This serial number is called the index of each element. As a rule, it is an integer, less often a symbol. In principle, for a data cell, it does not matter which indexation is set: numeric or alphabetic.
An array in Pascal (examples below) can have only one type, to which all its elements will belong. It does not happen that one cell is of type real and the other is integer.
In a programming environment, the data of a one-dimensional (i.e. simple) array is entered linearly:
Var a: array [5..40] of char;
b: real;
i: integer;
BEGIN
For i: = 5 to 40 read (ch [i]);
For i: = 5 to 40 write (ch [i]: 3);
Readln
END.
Parsing an example of programs in Pascal, you can see that memory is allocated for the simplest array of 35 characters. Indexing ([5..40]) - from 5 to 40. On the first line after the BEGIN command, the user must enter 35 absolutely any characters (numbers, letters) that the program writes to the cells. The second line is responsible for displaying them on the screen.
Two-dimensional arrays
If a one-dimensional array is one in which all operations are lined up, that is, all elements and actions are performed one after another, then a two-dimensional array allows for more complex branching structures.
Such data in Pascal (examples can be seen below) are described in two ways: “array [10..b, 10..f] of type” or “array [10..b] of array [10..f] of type. "
Variables b and f are constants. Instead, you can insert numbers (as in one-dimensional arrays), but in such tasks it is better not to practice this. This is because the programming language defines the first constant as the number of rows, and the second - the columns of this array.
An example of a task through a special type section:
Const b = 24; f is 13;
Type cherry = array [10..b, 10..f] of real;
Var n: cherry;
Using the variables section, describing the array is somewhat easier (for beginners):
Const b = 24; f is 13;
Var n: array [10..b, 10..f] of real;
Both options will be true, the program does not change in any way from the selected recording method.
Open arrays
An open array is one that has no boundaries. It has only a type (real, integer, etc.). In other words, the created array is dimensionless. Its “stickiness" is determined by the program itself during execution. It is written as follows:
Seulgi1: array of char;
Yeri: array of real;
A distinctive feature of these arrays is that indexing starts from zero, not unity.
Graphics in Pascal
Those who have at least the slightest idea of the "insides" of the operating system know that all images are built thanks to rectangular rasters. Raster images consist of pixels that are so small that the human eye perceives the drawn or photographed as a whole. In this case, the higher the resolution of the photo, the smaller the pixels will be.
The second way to present graphics is vector. As a rule, these are segments, circles, ellipses and other geometric shapes that make up the whole picture.
In a programming environment in any language, the appearance of such a kind of tasks as graphic is possible, the result of which will be graphics. Pascal, examples of such programs show to some extent the severity of their development, allows you to create pictures and drawings. In order to "activate" the desired library, you need to insert the line "uses graph;".
As a rule, when describing graphic data, this procedure is used:
InitGraph (var driver, choke: real; Path: string);
Here driver is a variable whose type is integer; choke - a variable of real type; and path allows drivers to be activated.