Pascal programming for beginners example programs. Pascal - Programming Basics for Beginners

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, allowing you to avoid 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. One of the most difficult topics in programming is also covered - the construction of 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 creation 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. We include real numbers 45 in the work
Description of the real data type (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
Lesson 2.3. How to combine integer and real type variables 51
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 ​​using a random number sensor 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
ASCII 67 code table
Description of the Char type 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
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
Application 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. Loop statement for 99
Operator for 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 the choice of operators while 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 random numbers and outputting the array to the screen 134
Creating a Custom Data Type 137
Finding the maximum array element 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. Saving 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. Enable 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 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
Creating your own data type - entry 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 data structures. 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
Graphics. Straight 303
Graphics. 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 included a large number of thematic tasks in the application, organized in blocks of 5-8 tasks. The tasks in each block are arranged from simple to complex. We use them in our lessons to organize practical exercises while reinforcing 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.

Let's create our first program in PascalABC.NET. Let's look at the main points.

PascalABC.NET – SCHOOL – Prime numbers. Sieve of Eratosthenes

Let's look at the fastest algorithm for finding prime numbers. Sieve of Eratosthenes. Experiment to compare algorithms. Let's look at a few more functions and capabilities of the Pascal language...

PascalABC.NET – SCHOOL – Compilers (1.Introduction) – Number extraction

Let's start understanding compilers. We look for whole and fractional numbers in the text. Stay tuned for updates to this line!

PascalABC.NET – SCHOOL – Compilers (2. Finish highlighting) – Words and strings

We continue to develop the compiler. Learning to highlight words and lines.

PascalABC.NET – SCHOOL – Solving a problem for 7th grade

PascalABC.NET – SCHOOL – ♫ Piano + OOP + Klitschko

Let's figure out how to make a simple piano using OOP (object-oriented programming).

PascalABC.NET - SCHOOL - 1. Cool calculator with brackets. Parsing expressions

We are writing a powerful calculator that parses parentheses and functions, such as sin or trunc. OOP (Object Oriented Programming).

PascalABC.NET - SCHOOL - 2. Cool calculator with brackets. Parsing expressions

CONTINUATION!!! We are bringing to life a powerful calculator that parses parentheses and functions, such as sin or trunc. OOP (Object Oriented Programming).

PascalABC.NET - SCHOOL - 1. Advanced phone book! Quick search. Indexing

How to implement quick search in a large amount of data. Graph theory. Let's create it using a phone book as an example.

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 conversations devoted to practical issues of programming and problem solving. Numerous examples allow you to better understand how to develop an algorithm, write your own program, and format its text correctly. Tips and notes draw readers' attention to important details, help them avoid pitfalls, and write programs more efficiently.
The book was written by school computer science teachers who have extensive experience of many years of practical work.

What is a programming language? Any problem that a computer solves is written 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 Nikolaus Wirth specifically for teaching students programming. The peculiarities of language also include its structure. That is, the program is easily divided into simpler, non-overlapping blocks, which, in turn, are divided into even simpler blocks. This also makes programming easier. In 1979, the language was approved as a standard language. Wirth named it after the French scientist Blaise Pascal, inventor of the calculating machine. The Pascal language is simple, logical and efficient. It has spread all over the world. Our conversations are based 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
Thanks 7
From the publisher 8
TOPIC 1. How to write a simple program in Pascal 9
Lesson 1.1. We display a message on the display screen 10
Lesson 1.2. How can I install this program on my computer? 11
Stages of creating a computer program 12
1. Launching the Pascal 14 environment
2. Working in the Edit 16 editing window
3. Saving the program to a file on disk 19
4. Launching the compiler 20
5. Execution of program 21
6. Viewing the results of the program 21
7. Exit from Pascal 22 environment
Lesson 1.3. Formatting text on screen 22
Conclusions 28
Test questions 28
TOPIC 2. How to incorporate numerical data into your work 30
Lesson 2.1. Let's start with something simple: the integers 31
Concept of variable 32
Type Integer. Assignment operator. Display 32
Operations with type Integer 34
Standard functions of type Integer 36
How variables of integer type are represented in computer memory 38
Lesson 2.2. We include real numbers 39 in the work
Description of the real data type (Real) 40
Formats for recording real variables 40
Material operations 41
Standard functions of type Real 41
Writing Math Expressions 43
How real type variables are represented in memory
computer 45
Lesson 2.3. How to combine integer and real type variables 46
Type conversion 46
Priority rules in performed actions 47
Actions with different types of data 47
Lesson 2.4. Data input and output 51
Entering variables from the keyboard 52
Beautiful display 52
Setting variable values ​​using a random number sensor 55
Lesson 2.5. Why are constants needed in a program? 57
Conclusions 59
Test questions 60
TOPIC 3. Learning to work with symbols 61
Lesson 3.1. How does a computer understand 62 characters?
ASCII 62 code table
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
Relation operations 70
Boolean I/O 71
Lesson 4.2. Logical (Boolean) operations 71
Logical multiplication (conjunction) 72
Logical addition (disjunction) 72
Exclusive OR (addition modulo 2) 73
Logical negation (inversion) 74
Using logical operations in program 74
Priority of logical operations 76
Conclusions 77
Test questions 78
TOPIC 5. Analysis of the situation and sequence of command execution 79
Lesson 5.1. Condition checking and branching in Algorithm 80
Full and incomplete form of the if statement 81
Design of programs 84
Lesson 5.2. Operator blocks 85
Lesson 5.3. Branching according to a number of conditions (case statement) 90
Conclusions 94
Test questions 95
TOPIC 6. Repeated actions 96
Lesson 6.1. Loop-statement for 97
For statement with incremental counter increment 97
For statement with sequential decrement of counter 99
Lesson 6.2. Using loops with a counter of 99
Cycle within a cycle 100
Tracing 101
Calculating the sum of series 103
Conclusions 107
Test questions 108
TOPIC 7. Loops with condition 109
Lesson 7.1. Loop with precondition 110
Description of the loop with precondition 110
Approximate calculation of the sum of an infinite series 111
Entering a number to the specified integer power 114
Lesson 7.2. Loop with postcondition 118
Description of a cycle with a postcondition 119
Using repeat and while loops 119
Relativity of the choice of operators while 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
Creating a Custom Data Type 137
Finding the maximum array element 141
Calculating the sum and number of array elements with given properties 146
Lesson 8.2. Search in array 148
Determining if an Array Has a Negative Element Using a Flag 149
Determining the presence of negative elements in an array by calculating their number 150
Finding the number of the negative array element 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. Designing a Top-Down Algorithm 161
Practical problem using auxiliary algorithms 162
Lesson 9.2. An example of working with a function: searching for the maximum element 169
Conclusions 171
Test questions 171
TOPIC 10. How to work with character strings 1 72
Lesson 10.1. Working with strings of characters: type String 1 73
Description of a string variable 173
Basic operations with 174 strings
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 180 parameters
The simplest procedures with 180 parameters
Formal and actual parameters 182
The simplest functions with 183 parameters
Lesson 11.2. Methods for passing parameters 184
Conclusions 187
Test questions 187
TOPIC 12. Files: saving the results of the work until 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 Numeric Data to a Text File 196
Saving an array of numbers in a text file 197
Adding information to the end of the file 201
Conclusions 202
Test questions 203
TOPIC 13. Graphic mode of operation. Graph 204 module
Lesson 13.1. Enable graphic mode 205
Features of working with graphics 205
Switching to graphics mode of 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 that change the natural flow of the program 213
Lesson 14.1. Using the goto 215 unconditional jump operator
Lesson 14.2. Statements that change the flow of a loop 218
break statement 2.19
Operator continue 220
Conclusions 220
Test questions 221
Appendix 1. Block diagram elements 222
Appendix 2. Homework 224
Assignments for Chapter 2 224
Assignments for Chapter 4 227
Tasks for chapters 6-7 229
Chapter 8 assignments 236
Alphabetical index 254

Professional development environment for creating programs and applications of any level of complexity. Combines the classic simplicity of Pascal with 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 a 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 practical video lessons is ideal for those who want to learn how to make a program in Pascal ABC, regardless of their skill level. Each lesson has its own topic, so you can watch them either in order or selectively to deepen and expand your knowledge in a particular area.

Pascal ABC Lessons

The Pascal ABS lessons presented in the video course are based on the development application programs and provide practical knowledge. All programs that you write during the video course are fully working and can be used in everyday life– there is no “water” or 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 design a timer.


We study 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 music synthesizer.


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



We create the “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 - Making 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)

Share