Learning pascal language. Pascal for beginners (3 lessons on basic types of algorithms)

Typically, learning any programming language begins with the Hello, world! But we'll get to this a little later, because now I want to tell you about quantities and data types in Pascal.

Types of quantities

Typically, learning any programming language begins with the Hello, world! But we'll get to this a little later, because now I want to tell you about quantities and data types in Pascal. What is magnitude? A quantity is a separate information object stored in computer memory. There are two classifications of quantities. We will look at each one separately.


We will talk about them in more detail in subsequent lessons.

Numeric, in turn, is divided into two types.


I immediately give the designation of this type in Pascal

Character ones are also divided into two types.

Second classification of quantities.
Constant - the value is unchanged. Variable - the value can change while the program is running. Variables are identified by symbolic names called identifiers. How to assign a value to a variable? Using the assignment command! Assignment command in Pascal: < переменная >:= < выражение >

First program in Pascal

Program structure in Pascal

Heading

Program< имя переменной >;

Declaring Variables and Constants

const<Символическое имя> = <выражение>;
var<Однотипные переменные> : <тип>;

Operators section

Begin
<оператор 1>;
<оператор 2>;

end.

Input and output in Pascal

You probably have a question: “How to enter variable values ​​from the keyboard?”, and also: “How to display the result obtained on the screen?” To do this, four special operators are used:

To enter:

  • read(Variable)
  • readln(Variable)

And for output:

  • write(Text in ")
  • writeln(Text in ")

“What is the difference between read and readln, write and writeln?”: you will be puzzled again. The answer is simple. If you add “ln” (which stands for Line), the cursor moves to the next line.

Hello, World in Pascal

Now you are ready to write your first program.

Program Hello_World; (curly quotes allow you to insert a comment to the code) Begin Write("Hello, World!"); End.

Well, everything is clear here. To diversify the solution, I propose to solve several problems of this type.

Problem 1. Required to prompt for username and then display "Hi,<имя>!» To solve this problem, we need a string data type - string.

Program Hello_boy_girl; (curly quotes allow you to insert a comment to the code) var name: string; (we declare name is a new variable) Begin Write("Enter your name: "); (since we are using write, the cursor will blink after the colon) Read(name); (We read everything that the user entered) Write("Hello,", name,"!"); (Please note! The variable is separated from the quotation marks by COMMA End. !}

Task 2.You need to find out how old the user is, and then display “You<возраст>years". Obviously, to solve this problem we need an integer data type - Integer.

Program How_old_are_you; var years: integer; (declare years is a new variable) Begin Writeln("How old are you?"); (since we are using writeln, the caret will blink under the caption) Read(years); (We read everything that the user entered) Write("You are ", years," years old."); End.

Well, that's it! Everything I wanted to tell you in today's lesson. You learned what a quantity is in Pascal and learned the basic types. We also learned how to create simple linear programs. We will talk in more detail about types in PascalABC.Net in the following lessons. By the way, about PascalABC.Net. It is clear that writing programs on pieces of paper is useless, and you should practice on a computer. Use this link to download the latest version for Windows.
This is what the program interface looks like when launched.


P.S. Let's highlight two features of Pascal:

  1. Pascal is NOT case sensitive, that is, program, Program, and PrOgRaM are no different for it. Therefore, from the very beginning, get used to writing in lower case, so that when switching to another programming language you do not experience any problems with this;
  2. The name of the program in Pascal does not matter; it does not carry any useful functions. If you want, you can simply not write HIM!

P.P.S. There are three most popular operating systems in the world: Linux, Mac OS and Windows. What if you don't have Windows? Which compiler should I choose then? We will talk about this in lesson number N.

The PascalABC programming environment is used as an initial training in programming in the Pascal programming language. The environment contains a powerful help system and a built-in problem book with auto-checked tasks.

The free mini-version of the problem book contains 270 tasks in all main sections. This is quite enough for independent mastery of the initial level of programming. You can download version 3.0 here . Currently this version is no longer supported and the developers are working on PascalABC.Net. So, let's start pascal abc training.

Appearance of the PascalABC program interface

The programming environment window looks standard. It contains a menu bar (1), below is a quick access toolbar (2). Next is the working area of ​​the code editor.


Appearance of the program interface

At the top there are shortcuts to open files with program texts (3). Simultaneous work with several texts greatly simplifies the creation of programs. You can copy and move sections of code from one file to another (4).

In program execution mode, you can see the data input and results output zone (5).

Working with PascalABC

After typing the program, you need to save it and set a name. To do this you can use the command File-Save As or the corresponding button on the toolbar.

Give a name different from the default one. This will make it easier for you to find the code you need in the future.

Launch

Now you can run the program to check its operation. Start command Program -Run or the green triangle button on the toolbar. The code is compiled before running. If there are errors in the code, a red line will appear describing the error.


Errors in the program

The cursor will indicate the location of the probable error, but this does not always coincide with its description. For example, in the screenshot above, the cursor points to the Y variable, but the error is contained in the line above. There is a missing semicolon character, which must appear at the end of each command line. After correcting the error, run the program again.

Stop

The program stops when it completes. But there may be cases when the program must be stopped forcibly. To do this, there is a “Stop” button on the toolbar or a command Program - Finish.

Setting up a text editor

By default, the code editor is set to a minimum font size. If you experience discomfort when typing, then go to the menu Service - Editor Settings. Set the desired font size.


Setting up the Pascal ABC editor

Working with an electronic problem book

Go to the Tools menu - Create program template. The following window will open (see below)

Here you need to write the name of the task group. We look at the list and enter the desired group exactly as it is written in the window list. After specifying the name of the task group, the prompt will change.

Now the number of available tasks in this group will be indicated or the numbers of specific tasks will be listed. You add a number to the group name without a space. Press the button. A tab with the selected task will open. The new file will contain automatically generated code. It cannot be deleted.

Now to view the text of the task, click on the program launch button.

Look at the window above. It is divided into three parts. The first part gives the condition of the task for which you need to write code. In the second part, numbers are indicated in yellow. This is the source data generated by the problem book. They will be read. The third part displays the result that will be obtained when executing your program. Now close the window and add the code.

Let's start execution. If there are no errors, we will get a window.

You need to run the program several times to pass all the tests. And only after passing all the tests the task will receive the status “Task completed!” Now you can move on to the next task.

On the vastness of the World Wide Web you can find a lot of programs in the Pascal language, but it is much more difficult to understand on what principle they work and how they are structured. Let's learn the basics of programming together!

Algorithmic language: basic concepts

In colloquial speech we use basic units: symbols and whole sentences. The algorithmic language also has a similar structure, only its components are called differently. We are talking about elementary constructions, expressions and operators. All these units form a hierarchical structure, since each subsequent element is formed from the previous one.

The symbols of an algorithmic language are the indivisible atoms used to write code.

Elementary structures are minimal units that have their own meaning.

Classic language expressions are formed from the two above-mentioned units and set the rules for finding the required meaning.

The operator is responsible for describing a certain transformation, which is mandatory for the correct execution of the program. There can be several of them, if necessary - the program must perform a complex operation. In such situations, they are combined into a block or a compound statement.

Pascal language

There are a large number of "Pascal" (there are separate manuals for beginners) is one of them. Its alphabet consists of numbers, letters and special characters. Here is their list:

  • 26 Latin capitals and ;
  • underscore;
  • ten digits;
  • limiters;
  • operation signs;
  • specifiers;
  • system-reserved (service) words.

In addition to the elements listed above, the basic set includes “space,” which cannot be used within the construction of reserved expressions and double characters.

Elementary language structures

"Pascal" for beginners includes strings, numbers and names.

Numbers used in the code of the programming language in question are usually written in the decimal system. They can be either real or integer, which are usually mentioned without a decimal point. If the number is positive, then its sign can be omitted.

Pascal is an algorithmic programming language in which strings are a sequence of characters enclosed in apostrophes. If you must use the apostrophe itself, it is worth mentioning this symbol twice.

A name is a sequence that begins with a letter and may contain numbers. Identifiers are usually called labels, types, constants, functions, procedures, variables, objects, and even modules. When forming identifiers, you can use the underscore character. The name can have a lot of characters, but the compiler will only read the first 63 characters. “Pascal,” the description of which may seem so complicated, is not so scary, so don’t rush to get scared and close the browser page!

It is prohibited to use standard names of constants, procedures, types, files, functions, as well as service expressions as language identifiers.

Spaces will help improve the clarity of the code, but it is worth remembering that they cannot separate names and numbers in the middle.

Algorithmic language syntax

Each line must end with a semicolon in a program written in the language we are considering (Pascal). Computer science teaches this to schoolchildren and students, and you can understand these rules yourself!

The semicolon (;) is a conditional signal that indicates the end of the current line and the need to move to a new one. But the exception may be service commands: const, var, begin and others.

The end statement closes the program, so it must be followed by a period. Sometimes the code may contain several attachments, then the beginning and end of the block will be separated by a semicolon.

To assign a specific value to a variable, you must place a colon before the equal sign. For example, you want to set n=13, but in the code it will look like n:=13.

If you learn these rules, you can quickly learn how to write program code without syntax errors.

Classic Pascal operators

You can program repeating code fragments of a future application and perform any actions with it using various methods. But the Pascal language uses various operators for this. We will not be able to consider all of them, so we will deal with only a few.

For example, using the selection operator, you can select one of the alternative paths for the program. The parameter in this case is an expression of ordinal type. But there is one caveat: this select key cannot be of type string or real.

There are also assignment operators, conditional operators, compound operators, and empty operators, as well as a host of other useful attachments. Knowing just a few of them allows you to write code with excellent functionality. Operators should not be overused, because their large number makes the program difficult to debug with a compiler, confusing and very difficult for outsiders to understand.

Assignment operator

This expression takes the form of a colon and an equal sign. It is used to assign a specific value to a particular variable. It is important to remember that the type of an expression and a variable must be the same unless they are an integer and a real type, respectively. Only in such a situation will direct transformation take place.

Compound Operators

Pascal is a programming language that uses sequences of arbitrary program statements enclosed in special parentheses. We are talking about compound structures limited by the words begin and end. It is an important tool in an algorithmic language that makes it possible to write code using a structured methodology.

The Pascal language operators that are part of a compound construction can be completely different, because there are no restrictions. The depth of nesting can also be different.

Conditional operator of a programming language

This component provides the ability to check a given condition during the program and perform an action depending on the results of its passage. Thus, a conditional command is one of the means of forming branches in the process of performing calculations.

Structurally it looks like this:

IF<условие>THEN<оператор1>ELSE<оператор2>.

In this expression, else, then and if are reserved words, the condition is a logical expression with arbitrary content, and the operators are any commands of the programming language used.

Program code structure

The header, statement and description sections are the key components of an application written in a language such as Pascal. Computer science allows you to fully explore these elements and learn how to use them correctly.

The header usually contains the name of the code. For example, Program MyFirst.

The descriptions section may include connected libraries, modules, labels, constants, types, variables, and a chapter describing functions and procedures.

The module description section contains the names of the included libraries and begins with the reserved word uses. It should come first among all other descriptions. Module names must be separated by commas.

You can put a label on any program code statement, the name of which should be mentioned in the corresponding section of the description.

Premature description of constants allows you to write their names later in the code instead of numeric or alphabetic values.

In the section describing the variables used, you should indicate all the types that will be used: "var c,a,r: integer; k,l, m: char; h1,h2: boolean;".

Do not forget that Pascal is a programming language that requires a mandatory preliminary description of all the components involved in the program.

The code text must end with a period.

Example programs

"Pascal" is an elementary language, and after studying the information described above, you can proceed directly to writing code.

Let's make the application display the phrase “It is my first program!”

The Pascal example programs are very important to understand, so try it now.

Begin
Writeln(It is my first program!");
End.

It's that simple!

Look at more complex code that can be used to find roots. Pay attention to the principle of forming computational expressions.

We hope you found it useful.

Today, many people are interested in whether it is possible to learn to program from scratch.

We have all heard stories that people who are engaged in this business have huge incomes, go to Bali every weekend and in the first months of their work were able to buy apartments for all their relatives.

In principle, this is not far from the truth, but Such results require experience and reputation. And you need to start with the simplest. We will look at what steps a person who knows nothing about writing programs needs to follow in order to travel to Bali and buy real estate in the future.

Contents:

Step one. Preparation

Often, beginning programmers cannot achieve success for the simple reason that they initially failed to set priorities correctly.

They present this craft as something romantic, dynamic - just some kind of constant action.

In films, this process is shown completely different from what it really is.

Moreover, there is generally The writing of codes is not displayed, we are only shown events that revolve around it.

The films also show that anyone can become a programmer without knowledge, experience, or even gray matter in the brain. In this case, we can recall the film “Frames”.

So if you are simply saturated with the spirit of all kinds of films and want to start “coding” yourself, programming is clearly not for you.

Here's the truth about the craft in question - programming is:

  • hours and sometimes days of monotonous work, during which you cannot relax, you must always be focused;
  • endless learning in pursuit of the latest trends in this area;
  • similar projects with customers who themselves do not know what they want and what it should look like.

As for the latter, you must It's worth watching a video about seven red lines of different colors, one of which is in the form of a kitten. In principle, this is close to the truth, since customers often demand the impossible. It also happens that they order something, but they are always not satisfied with the result.

https://youtu.be/nU0Fy5JXOtY

If you understand all this and are ready to plunge into the wonderful world of programming, then proceed to the next step.

Step two. Selecting your first language

There are a huge number of programming languages. According to some estimates, their number has already reached several thousand.

In general, C is one of the simplest languages ​​that provides the basis for everything else. Moreover, its elements are used in many other systems and programs.

But it is interesting that in good educational institutions, as well as During the courses, students study languages ​​in the following order:

1 Pascal.

2 C++.

3 PHP and everything related to web programming, as well as SQL(this is a system designed to work with databases through queries).

  1. Web(website development, online systems and everything connected with it) – html(although it cannot be called a full-fledged programming language), PHP, Perl, Python, Ruby, Java, Groovy, as well as ASP.NET technology.
  2. Custom software(all kinds of programs like reference books, browsers, instant messengers, etc.) – Delphi, C, C++, C#.
  3. Custom software for mobile devices– Java, Objective-C.
  4. Machine developments(working with microprocessors and other devices, robotics design) – Assembler, modifications C.

Someone can also add so-called 1C programming to this list. Don't trust laymen and people who don't know anything! This is not programming at all.

Once you become familiar with the basics of the work in question, you will understand why this can be said.

Choose what you like best.

Advice: Make your choice right away! You must know exactly in which direction you will develop and what to study in the future.

Most specialists also It is recommended to start your learning with Pascal. This option will allow

You need to write the simplest programs and have a general idea of ​​the craft in question as a whole.

We can say that Pascal is a kind of bridge. A person who simply knows mathematics well can move through it into the world of programming.

Attention! Whatever company you choose to work for after training, you will be retrained for yourself. Therefore, you just have to understand the very principle of writing programs. And for this there's nothing better than Pascal.

Step three. Studying Compilers

For reference: Compiler is a technical solution designed to translate input commands into machine instructions, roughly speaking, into zeros and ones, that is, into an interpretation in which the machine will understand what to do.

Actually, you will write and execute all your programs in compilers.

If you decide to follow our advice and start with Pascal, then you should download Free Pascal. This compiler is absolutely free and is distributed on the official website.

As you can see, it looks quite “old-fashioned”, but programming begins exactly with this. By the way, the C++ compiler looks almost the same.

It's called Turbo C++ (you can download it).

As for Pascal, there is also GNU Pascal, Turbo/Borland Pascal, TMT Pascal and Virtual Pascal. And for C++ you can use Borland C++, +, Dev C++, GCC and Eclipse.

But this, as we said above, is just the beginning. You can't stop there. Once you've made a choice about your direction, you can move on to more complex compilers.

Here is a list of the most popular compilers today depending on the areas of activity:

    RegardingDelphi, then that’s what the compiler is called there. There is also Embarcadero Delphi and some other modifications. Delphi 7 can be downloaded from many sites, for example. If you chose C, C++ or C#, then you need Microsoft Visual Studio. You can download it directly from the manufacturer's official website.

    If we talk aboutAssembler and other languages ​​used in robotics, then you need to immediately download MASM if you are working on Windows. In general, depending on the field of activity you choose and the company for which you get a job, compilers can be very different. Some firms write their own code processing solutions. Therefore, if you have chosen robotics, it is better to study the relevant books and do everything as they say. We'll talk about this later.

There are also many online compilers. They are useful because they support many programming languages ​​and do not require installation - very convenient!

Here are the most popular ones:

This is a unique service that allows you to create several virtual computers and do whatever you want on them, including compiling ciphers.

Virtual machines will run under . On them you can at least delete the system folder, install absolutely any program, and so on.

Now let's start writing your first cipher (code). This can be done even without books and long instructions.

Step four. First code

For the first code, we will use the first language and the first compiler, which we advised to choose above. These are Pascal and Free Pascal.

One of the simplest programs is written as follows:

1 Download Free Pascal follow the link above and run it on your computer.

2 Enter the following: “program [name];”. That is, if you want the program to be called “hello”, you must enter “program hello;”.

3 Enter the "begin" statement. This means that the code that will need to be executed later has begun.

4 We use one of the most common constructions in Pascal “writeln(‘[some text]’);”. It simply displays text on the screen. Which is contained in parentheses and quotation marks. We will introduce the combination "Hello, world!". This is usually where people start their journey into the big world of software development. So the next line would look like “writeln(‘Hello, world!’);”.

5 To complete the cipher being executed, enter "end."(necessarily with a period at the end).

6 Now press the button "F9" to run what you wrote. You will see the words appear on the screen "Hello, world!". This is what was required!

To get you started with other languages, books usually also provide instructions on how to write “Hello, world!” , that is, instructions that simply display such simple text on the screen.

So, you have mastered your first cipher! A start has been made. Now move on to intensive training.

Step five. Take online training

The advantage of online lessons is that you see everything clearly, from start to finish.

Therefore, it is better for beginners to start their journey with online trainings. Here best courses in Russian:

If you choose Python, check out McGrath's Python Programming for Beginners.

Step seven. Stay informed

When you have sufficiently studied the material in your chosen area and already have some experience, you need to constantly monitor the development trends of the software market.

Here's what we're talking about here:

All this is necessary so that you can rebuild in time.

Let's say you decide to do a certain type of programming, have studied the technology and are ready to go to work to submit your resume.

They look at your data, but they say that the company needs a specialist of a different profile.

So that you have as few such situations as possible, you should always know which areas of development are currently the most popular.

Important! Study both global trends and the situation in your city where you are going to work. If you want to be a freelancer, study orders on various exchanges and forums.

All this will allow you to develop and work in the right direction without wasting precious time.

This is very important in our time, since programming trends change not just in a few months, but in a few days!

If you studied certain methods of writing programs two or three years ago, Now all this is definitely irrelevant.

Step eight. Find a teacher

It is best to find a personal mentor who will tell you what and how best to do in a given case.

This option has many positive aspects, the main one of which is the presence of a personal approach to you personally.

Also, if problems arise, the teacher will help you solve them. A You can find a mentor in the following ways:

1 Go to university. Yes, training programs in post-Soviet countries may be quite outdated, but you can definitely grasp the basics, the very principle of programming, some basis for everything

2 Various courses. There are a huge number of courses where people gather in some office and the teacher, just like in a university, explains this or that concept. Typically, such courses are very intensive, and students quickly master the necessary information.

Of course, the services of a teacher will cost money, but your studies will be very productive, and you will be able to learn a lot of necessary information.

Step nine. Analyze other people's work

This is a very interesting method that allows you to develop very quickly. The fact is that understanding someone else's code is very difficult; only a few even professional programmers are capable of this.

If you do this at the very beginning of your business, you are sure to be able to reap great benefits in the future.

Moreover, this will allow you to become a project manager in the future, as you will have the ability to correct the work of other people.

More specifically, you need to do the following:

  • think about how you could optimize the cipher, that is, reduce the listing;
  • look for errors and shortcomings without and with a compiler;
  • Work and, accordingly, earnings are in your pocket! And if this is not the case, train further. At least you learned what tasks you need to complete to get hired.

    In any case, devote enough time to this and then you will succeed!

In this article I will tell you about the basic principles of working with the language. Pascal. The information posted here is simple and understandable for novice programmers. After studying this article, you will have a basic knowledge of Pascal, and your further learning process will be much easier than it could be.

Pascal is one of the most famous programming languages. Created in 1968 - 1969 by Swiss scientist Niklaus Wirth, it was named after the French mathematician and physicist Blaise Pascal, and is used to teach programming to this day.

1. Preparing for work

So, you decided to learn how to program and start with the language most suitable for this - Pascal. To get started, you must purchase (download) a compiler.

What exactly is this "compiler"? This is a special program that reads the program code and then translates it into machine code language. That is, in essence, all programming languages ​​are a way of “communicating” with a computer, and the compiler helps translate our “speech” into something that it understands.

There are many compilers for Pascal, the most famous of them: Pascal ABC, Free Pascal, Pascal ABC.NET, Turbo Pascal. All of these programs are good in their own way, but for me personally (the author), Pascal ABC (or Pascal ABC.NET) is very convenient, since it has a very convenient interface, detailed help, and even a collection of problems specially prepared for beginners.

2. Getting started

Well, the compiler window is open in front of us. A clean, white (or blue?) sheet. This is where our first programs will be born. Before starting work, you should remember that the Pascal language is a combination of three components: the alphabet, syntax (rules for writing language objects) and semantics (rules for using these objects).
The Pascal alphabet consists of:
  1. Uppercase and lowercase Latin letters: A...Z, a...z;
  2. Digits: 0...9;
  3. Special characters: + - * / =< >.,:; ‘ # () ( ) and their combinations: “>=” “
The task of syntax and semantics is to correctly write a section of the program and correctly compose its structure, otherwise our program will work incorrectly (or not work at all!).
You probably can’t wait to get to the most interesting part, so now we’ll try to write your first programs.

The program structure looks like this:

Program "program name"; - program title;
(Note: there is a semicolon at the end of the line, not all, but most)
Uses (library name); Libraries are included here, which provide additional capabilities when creating programs;
Label(label name); here, if necessary, labels are written that allow you to go to different places in the program (more on them later);
Const here we indicate variables with a constant value, for example, p=3.14;
Var here we list all the variables separated by commas, and then indicate the data type (Example: Var: K, L, M: integer; N, O: real; (if there are several types));
Begin (no semicolon here)
Next comes the main block of the program;
end. – end of the program (a dot is required after “end”, unless this is the end of the program and not operator brackets).

3. Creation of the first programs

You are now familiar with the basic structure of the program. It should be noted that of the above sections, only “Var”, “Begin” and “end” are mandatory, while the rest can be used if required during the execution of the task.

Open your compiler window and enter the following lines:

Program Programma1;
begin
end.

Click the "Run" button. Nothing happens? That’s right, because our program is “empty”, we did not indicate what it should do, so nothing happened. Typically, the operating principle of a program written in Pascal consists of three stages: data input – data processing – data output. Now let's get acquainted with the “write” operator. It serves just to output data without moving to a new line. Let's try to apply it, thereby making our program a little more complicated and interesting:

Program Programma1;
begin
write(" Pascal ");
end.

Actually, this is how it is used. Between apostrophes we can enter any text, on any layout, and after executing the program it will appear in the output window (or on the command line, depending on what compiler you have). In this case, the word “Pascal” should appear.

3.1. Operators write, writeln, read, readln
Now let's talk about data entry. We've already seen the write operator, but there are others as well. Writeln, for example, is used to output data with a newline. For what? Well, for example, to give some variable a value:

Program Programma1;
var A:integer;
begin
writeln("A= "); read(A); (enter a value and “attach” it to variable A)
write(A); (Output the value of variable A)
end.

As you can see, I have briefly described each action in curly braces. This is called a comment. In the future I will also use them for explanations.
In this example, the read operator is used to assign a value entered from the keyboard to a variable. We see that when executed, it read the string into variable A. And the other operator, readln, works differently. Using it, we can immediately enter the desired string, and it will be read into the variable:

Program Programma1;
var A:integer;
begin
readln(A);
write("A= ", A); (the line will look like this: “A = “entered value A” „)
end.

Now that you know a little about data entry, let's talk about what the data can be and how to process it.

3.2. Data types in Pascal
While you were reading this article, you probably already came across an integer that you didn’t understand a couple of times. Having carefully studied the basic structure of the program, you probably realized that this is a data type. But what does this mean? Let's take a closer look at this.

The source data, which is entered from the keyboard or read from a file, is located in variables, and they, in turn, are stored in RAM. The data type determines what kind of data can be stored and how much RAM it will take up. Data types are integer and real.

Integer data types (for integers):
- byte
The storage capacity for values ​​of this type is 1 byte. The range of values ​​for this type is from 0 to 255.
- word
Values ​​of this type already occupy 2 bytes of memory, and the range of values ​​is already larger: from 0 to 65535.
- integer (already familiar to us)
The values ​​also take up 2 bytes of memory, the range is the same size, but also includes negative numbers: -32786...32787.
-LongInt
The amount of memory occupied by a type value is 4 bytes. The range of values ​​fully corresponds to the name of the data type: from -2147483648 to 2147483647
-ShortInt
The type value consumes 1 byte of memory, the range is relatively small: -128…127.

Real data types (for numbers with a fractional part):
- Real
Occupied by a type value – 6 bytes. The number of significant figures is 11-12. (significant figures are exact figures, i.e. not rounded). Type value range: from 2.9*10-39 to 1.7*1038.
-Double
The type value size is 8 bytes. The number of significant figures is 15-16. Range: 5.0*10324…1.7*10308.
- Extended
Occupies 10 bytes. The number of significant figures is 19-20. Range: 3.4*10-4932…1.1*104932.
In addition to these, there is also a character data type (char) and even a logical data type (boolean), the variables of which can only take the values ​​true or false.

So, we have already learned a lot about data input and output. Now let's move on to the most difficult part - data processing.

3.3. Data processing. Mathematical operations. Conditions. Logical operations.
We have already learned how to enter data into the program, now let's try to learn how to process it. The first and most important thing that will be useful to us in this matter is the assignment operator. It is expressed like this: “:=”, and is used to assign a value to a variable. Examples: A:=1.5; B:=2+A. Now that we're familiar with the assignment operator, we can look at Pascal's math operations:
  1. Addition (+);
  2. Subtraction (-);
  3. Multiplication (*);
  4. Division(/);
  5. Integer division (div) – returns the integer part of the division (Example: 10 div 3 = 3);
  6. Remainder of division (mod) – returns only the remainder of division (Example: 5 mod 2 = 1);
In addition to the above, there are also the following operations and functions for working with numbers:

Abs(x) – returns the modulus of x;
sin(x) – sine of angle x (in radians);
cos(x) – cosine of angle x (in radians);
int(x) – returns the integer part of x;
random(number) – random number from 0 to a given one;
sqr(x) – square x;
sqrt(x) – square root of x;
inc(x) – increase x by 1;
dec(x) – decrease x by 1.

Terms
Conditions in Pascal play a very important role, especially if the program execution algorithm is branched. The condition is formulated as follows:

If (condition 1) then (action 1 - main) else (action 2 - alternative)
(if – if, then – then, else – otherwise)

When constructing conditions, use the logical operations and, not, or, xor:

And is an operand that combines several conditions into one. The action will only be executed if all the listed conditions are true.
program Conditions;
var a:integer;
begin
readln(a);
if (2*2=4) and (3+2=5) then a:=a+1 else a:=a-1;
write(a);
end.

In this example, we see that all the conditions listed through and are true, therefore only the first action, going through than, was performed. If at least one condition was not true, then the second action would be executed.

Not – a logical action with a condition from one part. If the condition is false, then the main action (first) will be performed, if true, then the alternative (second).

Program Conditions;
var b:integer;
begin
readln(b);
if not 5=4 then b:=b+1 else b:=b-1;
write(b);
end.

Condition 5=4 is false, therefore the first action will be performed.

Or (or) is a logical operator for a multi-part condition. The main action will be executed if at least one condition is true.
program Conditions;
var d:integer;
begin
readln(d);
if (5=4+1) or (5=4) then d:=d+1 else d:=d-1;
write(d);
end.

One of the conditions is true, so the program will go to the main action. The same thing will happen again if all the conditions are true. If neither condition is true, then an alternative action is performed.

Xor – With this operator, the main action is performed if only one condition is true. If more than one condition is true, or none of them are true, an alternative action will be performed.

Program Conditions;
var n:integer;
begin
readln(n);
if (6=4) xor (3=5-2) then n:=n+1 else n:=n-1;
write(n);
end.

(Note: Do not forget that the priority of logical operations is higher than mathematical ones, therefore, if some are combined with others, then it is advisable to separate mathematical operations with brackets so that errors do not occur during program execution.)

Now we are familiar with the basics of data processing. It remains to talk about some additional procedures and functions for managing the program, which will come in handy more than once in your further training in Pascal.

3.4. Procedures and functions for program management
Let's talk about the mark mentioned earlier. This procedure is very easy to use and allows, if necessary, to go to any part of the program, “jumping” one of the following parts, or, conversely, return to the previous part. Registering a label is very simple: first we describe the label in the labels section (see paragraph 3. Getting started. Program structure), and then the transition location is indicated, and the desired operator is marked with a label.

Program Mark;
label 1,2;
var A, B:integer;
begin
readln(A);
2: if A=5 then goto 1; (2: - action after moving to the corresponding label,)
A:=A+1; (goto - go to the label)
goto 2;
1: write(A);
end.

In this example, the program increases the entered number by 1 until it equals five. Here we can trace the effect of the mark.

Delay (time) – stop the program with a delay, time in quotes is indicated in milliseconds.
Readkey – stops the program before pressing a key, the value of the function is the code of the pressed key.
Exit – early completion of the procedure.
It should be noted that for delay, readkey, and exit to work, you must connect the crt module (uses crt).

Also watch the video: Pascal from scratch - the first program.

4. Conclusion

After reading this article, you have gained basic knowledge of the Pascal programming language. The basic concepts and principles of working with this language were laid out here in accessible and understandable formulations. Now the matter is in your hands. If you use this information correctly and continue to learn the Pascal language, you will soon be able to master it perfectly.

Having understood the principle of working with Pascal, you will be able to study other programming languages, and in the future write more “serious” programs than those you met while studying this article. Keep learning! Good luck!



Loading...
Top