Lesson 12. Loops in Pascal: repeat, while, for


Download and install the application

Download Turbo Pascal for Windows 10,8,7,XP:

Version x32:
Version x64:
When you have selected the file you need, you need to install it:

  1. Unpack the archive.
    • For x32 system: there is no installation file - after extracting the archive, go to the BIN folder and run Turbo.exe.
  2. For x64 system: after unpacking, run the installation file. Turbo Pascal will be installed on your system - a shortcut to launch the program will appear on your desktop.
  3. Installation completed.

There is nothing complicated, as you can see. After installing Turbo Pascal, we can start writing our first program.

Video on the topic: Lesson 1. Pascal from scratch. Variables and data types

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).

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 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); {Print 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 line, 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 memory capacity of values ​​of this type is 1 byte. The range of values ​​for this type: from 0 to 255. - word Values ​​of this type already take up 2 bytes of memory, and the range of values ​​is already larger: from 0 to 65535. - integer (already familiar to us) 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 The memory occupied by the type value is 6 bytes. The number of significant figures is 11-12. (significant figures are exact figures, that is, not rounded). Type value range: from 2.9*10-39 to 1.7*1038. — Double The size of the type value 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.

Conditions

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 – 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) – a logical operator for a condition with several parts. 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, “skipping” 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 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.

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!

Features of the language

Any variable in this language has its own type. This means that it can take on many meanings. You can also perform various operations on variables.

Language Basics

When using Pascal, adhere to strict typing. When describing a variable, its type is determined and this type cannot be changed anymore.

The purpose of a variable is to participate in the operations defined by its type. This should be taken into account when designing programs. This approach allows you to compile correctness checks, which allows you to achieve high program reliability.

Simple types are basic in this program. Composite types are formed from simple ones. For other types, reference ones are used.

To construct a complex type, reference and composite types are used.

To narrow the range of acceptable values, limited types are formed. To do this, it is enough for simple types to set a range of valid values ​​(for example, a variable is set to a value from 1 to 5, or from 1 to 100, or other options). In this program it will be written as: for var i:=1 to 5 do or for var i:=1 to 100 do.

Rating
( 2 ratings, average 5 out of 5 )
Did you like the article? Share with friends:
For any suggestions regarding the site: [email protected]
Для любых предложений по сайту: [email protected]