Examples of how to program in Pascal school curriculum. Fundamentals of Pascal Programming for Students

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 process learning will be much easier than it could be.

Pascal- one of the most known languages programming. 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 special program, which 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 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 very user-friendly 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 you start, remember that 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 letters 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 that provide additional features when creating programs;
Label(label name); here, if necessary, labels are written that allow you to go to different places programs (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 switching to 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 in command line, depending on what compiler you have). IN 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, to assign a value entered from the keyboard to a variable, use read operator. 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 random access memory. 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 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 fractional part):
- Real
Occupied by a type value – 6 bytes. The number of significant figures is 11-12. (significant figures are exact numbers, 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 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 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) – 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.

On in this example, the program increments 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 language Pascal programming. 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!

2nd ed. - St. Petersburg: 2011. - 320With.

This book is not a textbook, but rather an assistant in mastering the Pascal programming language, which all schoolchildren are introduced to in computer science lessons. It consists of lessons on practical issues programming and problem solving. Numerous examples allow you to better understand how to develop an algorithm, write own program , format its text correctly. Tips and notes help the reader pay attention to important details, avoiding pitfalls and write programs more efficiently. The book was prepared by computer science teachers at the school who have extensive experience of many years practical work . The second edition adds several new chapters on records, dynamic variables, the stack, queues, and lists. Also illuminated is one of the most difficult topics

in programming - building recursive algorithms. Format: pdf

(2011, 2nd ed., 320 pp.) Size:

14.5 MB Watch, download:

docs.google.com
Content
Preface to the second edition 15
Introduction 16
From the publisher 16 TOPIC 1. How to write a simple program
on Pascal 17
Lesson 1.1. We display a message on the display screen 18
Lesson 1.2. How to install this program on a computer 19
Stages of creating a computer program 20
Lesson 1.3. Formatting text on screen 28
Conclusions 34
Test questions 34
TOPIC 2. How to incorporate numerical data into your work 36
Lesson 2.1. Let's start with something simple: the integers 37
Concept of variable 38
Type Integer. Assignment operator. Display 38
Operations with type Integer 40
Standard functions of type Integer 42
How variables of integer type are represented
in computer memory 43 Lesson 2.2. Let's put it to work 45
real numbers Description real type
data (real) 45
Formats for recording real variables 46
Material operations 46
Standard functions of type real 47
Writing Math Expressions 48
How real type variables are represented in computer memory 50
Type Conversion 51
Priority rules in performed actions 52
Actions on data different types 53
Lesson 2.4. Data input and output 56
Entering variables from the keyboard 57
Beautiful display 57
Setting variable values ​​by sensor random numbers 61
Lesson 2.5. Why are constants needed in a program? 62
Conclusions 64
Test questions 64
TOPIC 3. Learning to work with symbols 66
Lesson 3.1. How does a computer understand symbols 67
Code ASCII table 67
Description of the Char type and standard features 68
Lesson 3.2. Type Char - ordinal type! 70
Conclusions 71
Test questions 72
TOPIC 4. George Boole and his logic 73
Lesson 4.1. One more type is needed - logical! 74
Boolean data type 75
Relational operations 75
Boolean I/O 76
Lesson 4.2. Logical (Boolean) operations 76
Logical multiplication (conjunction) 76
Logical addition (disjunction) 77
Exclusive OR (addition modulo 2) 77
Logical negation (inversion) 78
Using logical operations in program 78
Logical operation priority 80
Conclusions 81
Test questions 81
TOPIC 5. Analysis of the situation and sequence of command execution 82
Lesson 5.1. Checking conditions and branching in Algorithm 83
Full and partial form of the if statement 84
Design of programs 86
Lesson 5.2. Operator blocks 88
Lesson 5.3. Branching according to a number of conditions (case statement) 92
Conclusions 96
Test questions 96
TOPIC 6. Repeated actions 98
Lesson 6.1. Operator for loop 99
for statement with sequential increment of the counter 100 Operator for with sequential decrement of the counter 101
Lesson 6.2. Using loops with counter 101
Cycle within a cycle 102
Trace 103
Calculating the sum of series 105
Conclusions 108
Test questions 109
TOPIC 7. Loops with condition 110
Lesson 7.1. Loop with precondition 111
Description of the loop with precondition 111
Approximate calculation of the sum of an infinite series 112
Raising a number to the specified integer power 115
Lesson 7.2. Loop with postcondition 119
Description of a loop with postcondition 120
Using repeat and while loops 120
Relativity of choice while statements and repeat 123
Conclusions 129
Test questions 129
TOPIC 8. Arrays - structured data type 131
Lesson 8.1. Storing similar data in the form of a table 132
Basic steps for working with arrays 133
Description of an array in Pascal 133
Filling an array with random numbers and displaying the array on the screen 134
Creation custom type data 137
Search maximum element array 140
Calculating the sum and number of array elements with given properties 144
Lesson 8.2. Search in array 148
Determining the presence of a negative element in an array using a flag 148
Determining the presence of negative elements in an array by calculating their number 149
Finding the number of the negative element of the array 150
Lesson 8.3. Two-dimensional arrays 154
Conclusions 156
Test questions 157
TOPIC 9. Auxiliary algorithms. Procedures and functions. Structured programming 1 58
Lesson 9.1. Designing a top-down algorithm 159
Practical problem using auxiliary algorithms 160
Lesson 9.2. Example of working with the function: Finding the maximum element 167
Conclusions 168
Test questions 169
TOPIC 10. How to work with character strings 170
Lesson 10.1. Working with strings of characters: type String 171
Description of a string variable 171
Basic operations with 172 strings
Lesson 10.2. Some Pascal functions and procedures for working with strings 173
Using Library String Routines 173
Conclusions 175
Test questions 175
TOPIC 11. Procedures and functions with parameters 176
Lesson 11.1. Simple examples using subroutines with parameters 177
The simplest procedures with parameters 177
Formal and actual parameters 179
The simplest functions with parameters 179
Lesson 11.2. Methods for passing parameters 181
Conclusions 183
Test questions 184
TOPIC 12. Files: saving the results of the work until next time 185
Lesson 12.1. How to work with a text file 186
Opening a file for reading 186
Opening a file for writing 188
Lesson 12.2. Preservation two-dimensional array numbers in text file 192
Saving Numeric Data to a Text File 192
Saving an array of numbers in a text file 192
Adding information to the end of the file 196
Conclusions 197
Test questions 197
Topic 13. Graphical mode of operation. Graph module 199
Lesson 13.1. Turn on graphics mode works 200
Features of working with graphics 200
Switching to graphics mode of the video adapter 201
Lesson 13.2. We continue to explore the capabilities of the Graph 203 module
Drawing lines using the Graph 203 module
Drawing circles using the Graph 205 module
Conclusions 206
Test questions 207
Topic 14. Operators that change the natural flow of the program 208
Lesson 14.1. Using the goto 210 unconditional jump operator
Lesson 14.2. Statements that change the flow of a loop 213
break statement 213
Operator continue 214
Conclusions 215
Test questions 215
Topic 15. Grouping data: records 216
Lesson 15.1. Description of data type record 218
Lesson 15.2. When and how to wisely use 220 records
Creation own type data - record 220
Array of records 220
Join operator with 221
Data structure selection example 223
Records of records 224
Conclusions 225
Test questions and assignments 225
Topic 16. Dynamic variables 226
Lesson 16.1. Memory allocation 227
Lesson 16.2. Addresses 229
Lesson 16.3. Signs 230
Pointers to individual variables 230
Pointers to variable blocks 232
Lesson 16.4. Dynamic memory allocation 232
New and Dispose 233
Dynamic memory allocation for arrays 235
GetMem and FreeMem 236
Accessing elements of an array created dynamically 237
Variable length array 238
Conclusions 241
Test questions 242
Topic 17. Dynamic structures data. Stack 244
Lesson 17.1. Let's describe data type 245
Lesson 17.2. Creating a stack and basic stack operations 247
Adding an element to the stack (Push) 248
Popping an element from the stack (Pop) 251
Checking the stack for emptiness (StacklsEmpty) 252
Lesson 17.3. Using the 253 stack
Stack Programming Using a 255 Array
Conclusions 256
Test questions and assignments 256
Topic 18. Dynamic data structures. Queue 258
Lesson 18.1. Operating principle and description of the data type 259
Lesson 18.2. Basic queue operations 261
Adding an element to the queue (EnQueue) 261
Removing an element from a queue (DeQueue) 263
Checking the queue for emptiness (QueuelsEmpty) 264
Lesson 18.3. Using Queue 264
Programming a Queue Using an Array 267
Conclusions 269
Test questions 269
Topic 19. Dynamic data structures. One-way list 270
Lesson 19.1. Description of the data type and operating principle 271
Lesson 19.2. Basic operations with a one-way list 272
Sequential viewing of all list elements 272
Placing an element in a list 273
Removing an element from a list 275
Lesson 19.3. List processing 276
The feasibility of using a one-way list 278
Conclusions 280
Test questions 280
Topic 20. Recursion 281
Lesson 20.1. Description of principle 282
Lesson 20.2. Towers of Hanoi 285
Lesson 20.3. Structure of a recurrent subroutine 287
Lesson 20.4. An example of a recurrent solution to a non-recurrent problem 288
Lesson 20.5. An example of a recurrent solution to a recurrent problem 289
Conclusions 291
Test questions 291
Appendix 1. Block diagram elements 292
Appendix 2. Problems 295
Integer. Description. Enter. Conclusion. Operations 296
Real. Description. Enter. Conclusion. Operations and functions 296
Real. Writing and Evaluating Expressions 297
Char. Description. Enter. Conclusion. Functions 298
Boolean. Writing Expressions 298
Boolean. Evaluating Expressions 299
If. Simple comparisons. Min/max/average 300
If. Equations and inequalities with 300 parameters
For. Transfers 300
For. Calculations with Loop Counter 301
For. Overkill with comparisons 302
While-Repeat. Search 302
While-Repeat. Rows 303
Graphic arts. Straight 303
Graphic arts. Circles 304
Arrays. Filling, withdrawal, amount/quantity 305
Arrays. Permutations 305
Arrays. Search 306
Arrays. Checks 307
Arrays. Maximums 307
Subroutines without parameters 307
Lines. Part I 308
Lines. Part II 309
Subroutines with parameters. Part I 309
Subroutines with parameters. Part II 310
Subroutines with parameters. Part III 310
Files 311
Unidirectional list 312
Recursion 313

After the release of the first edition of the book, our colleagues and students began to increasingly contact us with a request to supplement the first edition with information about the most studied and in demand data structures. In this edition we have added several chapters on records, dynamic variables, stack, queue and lists. We also tried to cover one of the most difficult topics in programming - the construction of recursive algorithms.
In the application, we decided to abandon the homework collection with many options on several topics. Instead, we put a large number in the application thematic assignments, organized in blocks of 5-8 tasks. The tasks in each block are arranged from simple to complex. We use them in our lessons for organization. practical classes when consolidating theoretical material (one lesson - one block).
The authors express their deepest gratitude to one of their best students, Associate Professor of the Department of Security information systems SPbSUAP, Ph.D. Evgeny Mikhailovich Linsky for support, many useful tips and great help during the work on the second edition of the book.

(for grades 6-7)

Sometimes we hear from schoolchildren that Pascal is boring and uninteresting. In fact, the Pascal language is a kind of “multiplication table” for everyone who wants to get a “taste” of programming. Easy and understandable, it becomes the basis for mastering all other programming languages.

At school computer literacy Comp-As in Minsk we teach Pascal for children. We have developed the “Free Pascal Video Lessons” project to help schoolchildren see that programming is interesting and exciting!

What do free Pascal video tutorials provide?

Free video lessons are based on the school curriculum. Here you will find many solved problems and prepare your homework easily.

What can you learn from face-to-face classes At school?

On computer courses For schoolchildren, we study Pascal and other programming languages ​​in more depth: PHP, C, C++ and Java. Do you want to continue your education, become a programming ace, and in the future get a promising job in the IT field? Call us and come take courses at the Comp-As School!

How to study free video lessons?

It is impossible to speak a language if you only learn it from a textbook. In the same way, you cannot become a programmer without writing a single program. That is why we recommend that, after watching each of the free video lessons, you immediately proceed to practice - and check the operation of the programs on your computer.

6th grade

7th grade

We invite you on a journey through the country of programming!
“Programming can be compared to a huge country, full of wonders, surprises, wonderful discoveries and even... dangers! A novice user or an experienced “gamer” - the winner of virtual monsters - does not always know that, having mastered even the basics of programming, he can make the computer draw pictures that are only slightly inferior in beauty to the masterpieces of the game. computer graphics, or play a melody while winking with the light indicators of your keyboard.

Well, what’s not a wonderful prospect - the solution to a complex programming problem, a solution that you and no one else could find, and now your program is “downloaded” via the Internet by hundreds and thousands of users, passing on the name of its creator to each other!

Well, what about the dangers? There are also dangers. The history of programming is marked by the mistake of the programmer who created control program for the American interplanetary probe and “filled in” a period instead of a comma. The comma lost its tail, and the interplanetary probe “missed” Venus by several million kilometers. What if something similar happens in a program that controls, for example, the flight of a combat missile?”