An introduction to the Pascal programming language. First level. Pascal - programming basics for beginners Pascal programming for beginners sample programs

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

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 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 Integer Type 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 \u200b\u200bby 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 the 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
XOR (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
Calculating 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. Top-Down Algorithm Design 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
Description of string variable 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 Transfer Methods 181
Conclusions 183
Test questions 184
TOPIC 12. Files: we save the work results 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 Numerical 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
Security questions 197
Topic 13. Graphic mode of operation. Graph 199 module
Lesson 13.1. Turn on graphic mode 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 modifying 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
Join operator with 221
Data structure selection example 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 memory allocation 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
Queue programming with array 267
Conclusions 269
Test questions 269
Topic 19. Dynamic data structures. Unidirectional 270 list
Lesson 19.1. Description of data type and operating principle 271
Lesson 19.2. Basic Operations with a Unidirectional List 272
Sequential Review of All Items in a List
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. The 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 Functions 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 301 cycle counter
For. Bust with 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 cover 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 simple 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 Information Systems Security at St. Evgeny Mikhailovich Linsky for his support, a lot of useful advice and great help when working on the second edition of the 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 better for beginners than learning other programming languages.

The course of seven hands-on video tutorials is ideal for those looking to learn how to do 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 the programs that you write in the course of the video course are completely working and they can be used in everyday life - there is no "water" and 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 - Convenient 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)

The PascalABC programming environment is used as an initial training in programming for schoolchildren in the Pascal programming language. The environment contains a powerful help system and a built-in problem book with auto-checked tasks. This will allow you to quickly learn how to write programs in Pascal.

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

PascalABC interface appearance

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


The appearance of the program interface

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

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

Working with PascalABC

After typing the program text, 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 different name from the default. This will make it easier for you to find the code you need later.

Running

Now you can run the program to test its work. Run 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 with a description of the error will appear.


Errors in the program

The cursor will indicate the location of the likely error, but this does not always match its description. For example, in the screenshot above, the cursor points to the Y variable, but the error is in the line above. The "semicolon" character is missing, which must be present at the end of each command line. After fixing the error, run the program again.

Stop

The program stops upon its completion. But there are cases when the program must be stopped forcibly. To do this, there is a "Stop" button on the toolbar or the command Program - Finish.

Setting up a text editor abc pascal

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


Customizing the Pascal ABC editor

Working with an electronic problem book

Learning to write pascal programs is impossible without practice. The system of automatic testing of the program for pascal will help you master programming in the pascal language.
Go to the menu Tools - Create program template. You will see the following window (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 tooltip will change.

The number of available jobs in this group will now be listed, or specific job numbers will be listed. You add the number to the group name without a space. Push the button. A tab with the selected task will open. The new file will contain the automatically generated code. You cannot delete it.

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

Take a look at the window above. It is divided into three parts. The first part gives the condition of the problem for which you need to write code. In the second part, numbers are indicated in yellow. This is the raw 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 we close the window and add the code.

We launch it for execution. If there are no errors, then we 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 proceed to the next task.

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 on practical programming and problem solving. Numerous examples make it possible to better understand how to develop an algorithm, write your own program, and correctly format its text. Tips and notes draw readers' attention to important details, avoid pitfalls, and write programs more efficiently.
The book was written by school teachers of computer science with extensive experience of many years of 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 of these languages \u200b\u200bis 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 can be easily broken 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. Pascal is simple, logical and effective. It spread all over the world. Our conversations are based on specific program examples. 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 a 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 creating a computer program 12
1. Start the environment Pascal 14
2. Working in the Edit window 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 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 variables of integer and real type 46
Type Conversion 46
Action Priority Rules 47
Actions on data of different types 47
Lesson 2.4. Data input and output 51
Entering variables from the keyboard 52
Beautiful Display 52
Setting variable values \u200b\u200bby 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
Char Type Description 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 (Boolean) 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. The for loop operator 97
The for statement incrementing the counter 97
For statement with successive counter decrement 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 into 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 119
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 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
Determination of 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 of 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 Pass Methods 184
Conclusions 187
Test questions 187
TOPIC 12. Files: we save the work results 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 in a text file 196
Saving Numerical 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 module 209
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
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

Did you like the article? To share with friends: