Learning programming from scratch pascal. An introduction to the Pascal programming language. First level. Working with an electronic problem book

Professional development environment for creating programs and applications of any complexity. It combines the classic simplicity of Pascal and 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 the school computer science course, giving students a basic knowledge of operators and variables. Thus, learning Pascal abs is given to beginners better than mastering other programming languages.

The course of seven hands-on video tutorials is ideal for those looking to learn how to make a program in Pascal ABC, regardless of skill level. Each lesson has its own topic, so they can be watched either in order or selectively to deepen and expand your knowledge in a particular area.

Pascal ABC Lessons

The lessons of Pascal ABS presented in the video course are based on the development of applied programs and provide practical knowledge. All programs that you write in the process of passing the video course are fully working and they can be used in Everyday life- There is no "water" and an 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 construct a timer.


Examining 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 musical synthesizer.


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



We create a "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 - Building a piano
Lesson 6 - Advanced Calculator (Part 1)
Lesson 6 - Advanced Calculator (Part 2)
Lesson 7 - Comfortable 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)

This article will describe the basics of the Pascal programming language required for writing the first programs: program structure, the concept of a variable, data types, mathematical operators and functions, the assignment operator, data input and output. Once again, I will emphasize that this article is for the very first steps in learning a language for students in grades 7-8. There will be no in-depth consideration here (there is a corresponding literature for this).

Program structure

The structure of a program is a collection of sections that make up a program.

To write the first program in Pascal, it is enough to know two sections (in fact, there are more of them):

  • variable declaration section - var- this section lists the names of the variables used in the program, separated by commas. The following is their type.
  • program body - starts with a word begin and ends with the word end.(with a dot). In this section, the text of the program itself is written
var variables: data type; begin body of the program end.

Variables

What is a variable.

Think of a variable as a memory location to which we assign a name and in which we can store something (number or text).

Memory cells named a, b, c

The variable name must meet the following requirements:

  • consist of letters of the Latin alphabet (a-z, A-Z), numbers and the underscore "_";
  • a variable name must not start with a digit (but can start with a "_" ( For example: _primer).
  • variable name must not contain spaces

Variable primer and PriMer for Pascal are equivalent

Data types

After we list the variables in the section var, we must specify their type:

  • integer- integer type
  • real- real type (fractional numbers)
  • string- string type

For example:

var a, b, c: integer;

where a, b, c are variables, integer is the type of these variables. Those. variables (memory cells) a, b, c can contain only integers.

There are many other types of data, but the three voiced ones are enough to write the first programs.

If you want some of the variables to be of one type, and some of the other:

var a, b: integer; with: real;

those. variables a, b Are integers, and the variable with- real number (non-integer).

Assignment operator

The assignment operator is used to assign a value to a variable.

:= assignment operator

Recording a: = 23; reads as "Variable a assigned value 23 ". Now in a memory location named a the number is stored 23.

Input operator

There is one more operator with which you can write a value to a variable, but using the keyboard.

readln (a)

As soon as pascal will execute the command readln (a), it will require us to enter a value from the keyboard, which will be written to the variable in parentheses. In our case, into a variable a.

Mathematical operations

+ - addition operation

- subtraction operation

* - multiplication operation

/ - division operation

mod- remainder of the division

div- whole part from division

Example:

S: = 22 mod 5; After executing the given when the variable S will become equal 2 .

S: = 22 div 5; After executing this code, the variable S will become equal 4.

Output operator

To display the value of a variable on the screen, use the command write (a) or writeln (a)... After executing the command writeln there is a transition to a new line, after the execution of the write command, it does not happen.

If you need to display text, then it is enclosed in apostrophes:

writeln (‘Mom washed the frame’);

You can also display the text along with the value of the variable:

a: = 6;
writeln (‘ The value of the variable a = ‘, a);

On the screen we will see: The value of the variable a = 6.

Consider the problem:

Find the area and perimeter of the rectangle using the length and width values ​​entered from the keyboard.

var a, b, S, P: integer; // declare variables begin writeln (" Enter the length of the rectangle"); readln ( a); // enter the length writeln (" Enter the width of the rectangle"); readln ( b); // enter the width S: = a * b; // calculate the area of ​​the rectangle P: = 2 * (a + b); // calculate the perimeter of the rectangle writeln (" The area of ​​the rectangle is ",S); // display writeln (" The perimeter of the rectangle is ",P); end.

This book is not a textbook, but rather an assistant in mastering the programming language Pascal, which all schoolchildren get acquainted with in computer science lessons. It consists of conversations dedicated to practical issues programming and problem solving. Numerous examples allow you to better understand how to develop an algorithm, write own program, correctly arrange its text. Tips and notes draw readers' attention to important details, allow you to avoid pitfalls, write programs more efficiently.
The book was written by school teachers of computer science with extensive experience of many years practical work.

What is a programming language? Any task that a computer solves is recorded as a sequence of commands. This sequence is called a program. The commands, of course, must be presented in a language that the computer can understand. One such language is the Pascal programming language. It was developed by Swiss professor Niko-laus Wirth specifically to teach programming to students. The features of the language also include its structure. That is, the program is easily split into simpler, non-overlapping blocks, which, in turn, into even simpler blocks. It also makes programming easier. In 1979, the language was approved as the standard. Wirth named it after the French scientist Blaise Pascal, the inventor of the calculating machine. The Pascal language is simple, logical and effective. It spread all over the world. Our conversations are built on specific examples programs. There are no lengthy theoretical explanations, so it is imperative to carefully read the comments in the program texts!
So, we start the first conversation right away with the first program in Pascal;

Content
Introduction 7
Acknowledgments 7
Publisher 8
TOPIC 1. How to write simple program in Pascal 9
Lesson 1.1. Displaying a message on the display screen 10
Lesson 1.2. How to put this program into a computer? eleven
Stages of creation computer program 12
1. Starting the environment Pascal 14
2. Working in a window edit Edit 16
3. Saving the program to a file on disk 19
4. Running the compiler 20
5. Program execution 21
6. Viewing the results of the program operation 21
7. Exit from environment Pascal 22
Lesson 1.3. Styling Text on Screen 22
Conclusions 28
Test questions 28
TOPIC 2. How to include in the work of numerical data 30
Lesson 2.1. Let's start simple: integers 31
Variable concept 32
Integer type. Assignment operator. Display 32
Integer Operations 34
Standard Functions of Integer Type 36
How Integer Variables Are Represented in Computer Memory 38
Lesson 2.2. We include in work real numbers 39
Description of the real data type (Real) 40
Recording Formats for Real Variables 40
Real operations 41
Real Type Standard Features 41
Writing Math Expressions 43
How real variables are represented in memory
computer 45
Lesson 2.3. How to combine integer and real variables 46
Type Conversion 46
Action Priority Rules 47
Data actions different types 47
Lesson 2.4. Data input and output 51
Entering variables from the keyboard 52
Beautiful Display 52
Setting the values ​​of variables by a random number generator 55
Lesson 2.5. Why do we need constants in a program? 57
Conclusions 59
Security questions 60
TOPIC 3. Learning to work with symbols 61
Lesson 3.1. How a computer understands symbols 62
ASCII code table 62
Description of the Char type and standard functions 63
Lesson 3.2. The Char type is an ordinal type! 64
Conclusions 66
Test questions 67
TOPIC 4. George Boole and his logic 68
Lesson 4.1. One more type is needed - logical! 69
Boolean data type 70
Relationship Operations 70
Boolean I / O 71
Lesson 4.2. Logical (Boolean) Operations 71
Logical multiplication (conjunction) 72
Logical addition (disjunction) 72
Exclusive OR (mod 2 addition) 73
Logical negation (inversion) 74
Using logical operations in a program 74
Boolean Priority 76
Conclusions 77
Test questions 78
TOPIC 5. Analysis of the situation and the sequence of command execution 79
Lesson 5.1. Condition checking and branching in Algorithm 80
Complete and incomplete form of the if statement 81
Programming 84
Lesson 5.2. Operator blocks 85
Lesson 5.3. Branching on a number of conditions (case statement) 90
Conclusions 94
Test questions 95
TOPIC 6. Repeated actions 96
Lesson 6.1. Operator- for loop 97
The for statement incrementing the counter 97
The for statement with successive decrement of the counter 99
Lesson 6.2. Cycles with counter 99
Cycle within cycle 100
Trace 101
Calculation of the sum of a series 103
Conclusions 107
Test questions 108
TOPIC 7. Loops with condition 109
Lesson 7.1. Loop with precondition 110
Description of a loop with a precondition 110
Approximate calculation of the sum of an infinite series 111
Inserting a number to a specified integer power 114
Lesson 7.2. Loop with postcondition 118
Description of a loop with a postcondition 119
Using repeat and while loops
Choice relativity while statements and repeat 123
Conclusions 129
Test questions 129
TOPIC 8. Arrays - Structured Data Type 131
Lesson 8.1. Storing the same type of data in the form of a table 132
Basic Steps for Working with Arrays 133
Description of an array in Pascal 133
Filling the array random numbers and displaying the array on the screen 134
Creation custom type data 137
Finding the maximum element of an array 141
Calculating the Sum and Number of Elements of an Array with Specified Properties 146
Lesson 8.2. Searching an Array 148
Determining the presence of a negative element in an array using a flag 149
Determining the presence of negative elements in an array by calculating their number 150
Finding the number of negative element of array 152
Lesson 8.3. Two-dimensional arrays 156
Conclusions 158
Test questions 159
TOPIC 9. Auxiliary algorithms. Procedures and functions. Structured programming 160
Lesson 9.1. Top-Down Algorithm Design 161
Practical problem using auxiliary algorithms 162
Lesson 9.2. An example of working with a function: finding the maximum element 169
Conclusions 171
Test questions 171
TOPIC 10. How to work with character strings 1 72
Lesson 10.1. Working with character strings: type String 1 73
String Variable Description 173
Basic Line Operations 174
Lesson 10.2. Some Pascal Functions and Procedures for Working with Strings 175
Using Library String Routines 175
Conclusions 177
Test questions 178
TOPIC 11. Procedures and Functions with Parameters 179
Lesson 11.1. Simple examples using subroutines with parameters 180
The simplest procedures with parameters 180
Formal and Actual Parameters 182
The simplest functions with parameters 183
Lesson 11.2. Parameter Passing Methods 184
Conclusions 187
Test questions 187
TOPIC 12. Files: we save the results of work until the next time 189
Lesson 12.1. How to work with a text file 190
Opening a File for Reading 190
Opening a File for Writing 193
Lesson 12.2. Saving a two-dimensional array of numbers to text file 196
Saving Numeric Data in a Text File 196
Saving an Array of Numbers in a Text File 197
Append information to the end of the file 201
Conclusions 202
Security questions 203
TOPIC 13. Graphic mode of operation. Graph 204 Module
Lesson 13.1. Turn on graphic mode 205
Features of working with graphics 205
Switching to graphics mode for the video adapter 206
Lesson 13.2. We continue to explore the capabilities of the Graph 208 module
Drawing lines using the Graph 209 module
Drawing circles using the Graph 210 module
Conclusions 212
Test questions 212
TOPIC 14. Operators modifying the natural course of the program 213
Lesson 14.1. Using the goto 215 unconditional jump operator
Lesson 14.2. Operators Changing Loop Progress 218
Break statement 2.19
The continue statement 220
Conclusions 220
Security questions 221
Appendix 1. Elements of block diagrams 222
Appendix 2. Homework 224
Chapter 2 Assignments 224
Chapter 4 Assignments 227
Chapter 6-7 Quests 229
Chapter 8 Assignments 236
Index 254

2nd ed. - SPb .: 2011. - 320with.

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

Format: pdf(2011, 2nd ed., 320s.)

The size: 14.5 MB

Watch, download: docs.google.com

Content
Preface to the second edition 15
Introduction 16
Publisher 16
TOPIC 1. How to write a simple program in Pascal 17
Lesson 1.1. Displaying a message on the display screen 18
Lesson 1.2. How to put this program into a computer 19
Stages of creating a computer program 20
Lesson 1.3. Styling Text on the Screen 28
Conclusions 34
Test questions 34
TOPIC 2. How to include numerical data in the work 36
Lesson 2.1. Let's start simple: integers 37
Understanding a variable 38
Integer type. Assignment operator. Display 38
Integer Operations 40
Standard Functions of Type Integer 42
How Integer Variables Are Represented
in computer memory 43
Lesson 2.2. We include in the work real numbers 45
Description of the real data type (real) 45
Recording Formats for Real Variables 46
Real operations 46
Standard features like real 47
Writing Math Expressions 48
How Real Variables Are Represented in Computer Memory 50
Lesson 2.3. How to Combine Integer and Real Variables 51
Type Conversion 51
Action Priority Rules 52
Actions on data of different types 53
Lesson 2.4. Data input and output 56
Entering variables from the keyboard 57
Beautiful Display 57
Setting variable values ​​by a random number generator 61
Lesson 2.5. Why do we need constants in a program? 62
Conclusions 64
Test questions 64
TOPIC 3. Learning to work with symbols 66
Lesson 3.1. How a computer understands symbols 67
ASCII code table 67
Char Type Description and Standard Functions 68
Lesson 3.2. The Char type is an 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
Relationship Operations 75
Boolean I / O 76
Lesson 4.2. Logical (Boolean) Operations 76
Logical multiplication (conjunction) 76
Logical addition (disjunction) 77
Exclusive OR (mod 2 addition) 77
Logical negation (inversion) 78
Using logical operations in a program 78
Boolean priority 80
Conclusions 81
Test questions 81
TOPIC 5. Analysis of the situation and the sequence of command execution 82
Lesson 5.1. Condition Checking and Branching in Algorithm 83
Complete and incomplete form of the if statement 84
Programming 86
Lesson 5.2. Operator blocks 88
Lesson 5.3. Branching on a number of conditions (case statement) 92
Conclusions 96
Test questions 96
TOPIC 6. Repeated actions 98
Lesson 6.1. Loop operator for 99
The for statement with a sequential increase in the counter 100 The for statement with a sequential decrease in the counter 101
Lesson 6.2. Using Counter Loops 101
Loop within loop 102
Trace 103
Calculation of the sum of a series 105
Conclusions 108
Test questions 109
TOPIC 7. Loops with condition 110
Lesson 7.1. Loop with precondition 111
Description of a loop with a precondition 111
Approximate calculation of the sum of an infinite series 112
Raising a number to a specified integer power 115
Lesson 7.2. Loop with postcondition 119
Description of the loop with postcondition 120
Using repeat and while loops 120
Relative selection of while and repeat statements 123
Conclusions 129
Test questions 129
TOPIC 8. Arrays - Structured Data Type 131
Lesson 8.1. Storing the same type of 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
Creating a Custom Data Type 137
Finding the maximum element of an array 140
Calculation of the sum and number of alements of an array with given properties 144
Lesson 8.2. Searching an Array 148
Determining the presence of negative alement in an array using checkbox 148
Determination of the presence of negative alements in the array by calculating their number 149
Finding the number of negative alement of an 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. An 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 character strings: the String type 171
String Variable Description 171
Basic Line Operations 172
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 of Using Subroutines with Parameters 177
The simplest procedures with parameters 177
Formal and Actual Parameters 179
Basic Functions with Parameters 179
Lesson 11.2. Parameter Passing Methods 181
Conclusions 183
Test questions 184
TOPIC 12. Files: we save the results of work until the 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. Saving a two-dimensional array of numbers in a text file 192
Saving Numeric Data in a Text File 192
Saving an Array of Numbers in a Text File 192
Append information to the end of the file 196
Conclusions 197
Test questions 197
Topic 13. Graphic mode of operation. Graph 199 module
Lesson 13.1. Turn on the graphical mode of operation 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 changing the natural course of the program 208
Lesson 14.1. Using the goto 210 unconditional jump operator
Lesson 14.2. Operators Changing Loop Progress 213
Break statement 213
The continue statement 214
Conclusions 215
Security questions 215
Topic 15. Grouping data: records 216
Lesson 15.1. Record 218 data type description
Lesson 15.2. When and how to use 220 records wisely
Create your own data type - record 220
Array of records 220
Attach Operator with 221
Example of choosing a data structure 223
Recordings 224
Conclusions 225
Test questions and tasks 225
Topic 16. Dynamic variables 226
Lesson 16.1. Memory allocation 227
Lesson 16.2. Address 229
Lesson 16.3. Pointers 230
Pointers to Individual Variables 230
Pointers to Variable Blocks 232
Lesson 16.4. Dynamic selection memory 232
New and Dispose 233
Dynamic memory allocation for arrays 235
GetMem and FreeMem 236
Accessing Elements of a Dynamically Created Array 237
Variable Length Array 238
Conclusions 241
Test questions 242
Topic 17. Dynamic data structures. Stack 244
Lesson 17.1. Let's describe the data type 245
Lesson 17.2. Stack Creation and Basic Stack Operations 247
Adding an item to the stack (Push) 248
Popping an item from the stack (Pop) 251
Checking the stack for emptiness (StacklsEmpty) 252
Lesson 17.3. Stack Usage 253
Stack programming with array 255
Conclusions 256
Test questions and tasks 256
Topic 18. Dynamic data structures. Queue 258
Lesson 18.1. Principle of operation and description of data type 259
Lesson 18.2. Basic Queue Operations 261
Adding an item to the queue (EnQueue) 261
Retrieving an item from the queue (DeQueue) 263
Checking the queue for emptiness (QueuelsEmpty) 264
Lesson 18.3. Using the 264 queue
Programming a Queue Using an Array 267
Conclusions 269
Test questions 269
Topic 19. Dynamic data structures. Unidirectional 270 list
Lesson 19.1. Description of data type and principle of operation 271
Lesson 19.2. Basic Unidirectional List Operations 272
Sequential Review of All the Items in a List 272
Placing an Item in a List 273
Removing an Item from a List 275
Lesson 19.3. List Processing 276
The Feasibility of Using a Unidirectional 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 of a recurrent problem 289
Conclusions 291
Security questions 291
Appendix 1. Elements of block diagrams 292
Appendix 2. Objectives 295
Integer. Description. Input. Output. Operations 296
Real. Description. Input. Output. Operations and Features 296
Real. Writing and Evaluating Expressions 297
Char. Description. Input. Output. Functions 298
Boolean. Writing Expressions 298
Boolean. Evaluating Expressions 299
If. Simple comparisons. Min / max / average 300
If. Equations and inequalities with parameters 300
For. Transfers 300
For. Calculations with a loop counter 301
For. Idle Comparisons 302
While-Repeat. Search 302
While-Repeat. Rows 303
Graphics. Straight 303
Graphics. Circles 304
Arrays. Filling, withdrawal, amount / amount 305
Arrays. Permutations 305
Arrays. Search 306
Arrays. Checks 307
Arrays. Highs 307
Subroutines without parameters 307
Strings. Part I 308
Strings. 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 contact us more and more often with a request to supplement the first edition with information about the most studied and demanded data structures. In this edition, we have added several chapters on records, dynamic variables, stack, queue, and lists. We also tried to highlight one of the most difficult topics in programming - the construction of recursive algorithms.
In the appendix, we decided to abandon the homework collection with many options on several topics. Instead, we put a large number of thematic tasks into the application, organized in blocks of 5-8 tasks. The tasks in each block are arranged from easy to difficult. We use them in our lessons for organizing practical lessons while 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 SPbGUAP, Ph.D. Evgeny Mikhailovich Linsky for support, many useful tips and a lot of help in the work on the second edition of the book.