Pascal - programming basics for beginners. An introduction to the Pascal programming language. Beginner level Pascal programming language for schoolchildren

This article will describe the basics of the Pascal programming language required to write 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 variable names used in the program, separated by commas. Next, their type is indicated.
  • 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.

Let's imagine a variable as a memory location that we assign a name to 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 from - real number (non-integer).

Assignment operator

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

:= assignment operator

Recording a: \u003d 23; reads as "Variable and assigned value 23 ". Now in a memory location named andthe number is stored 23.

Input operator

There is another operator that can be used to 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: \u003d 22 mod 5; After executing the given when the variable S will become equal 2 .

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

Inference statement

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

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: \u003d 6;
writeln (‘ The value of the variable a \u003d ‘, a);

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

Consider the problem:

Find the area and perimeter of the rectangle using the length and width values \u200b\u200bentered 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: \u003d a * b; // calculate the area of \u200b\u200bthe rectangle P: \u003d 2 * (a + b); // calculate the perimeter of the rectangle writeln (" The area of \u200b\u200bthe rectangle is ",S); // display writeln (" The perimeter of the rectangle is ",P); end.

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 make a program in Pascal ABC, regardless of skill level. Each lesson has its own topic, so you can watch them both in order and 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.

In the free mini-version of the problem book, 270 tasks are available in all main sections. This is quite enough for self-mastering the entry-level programming. You can download Pascal for free version 3.0 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 the quick access toolbar (2). Next is the work area of \u200b\u200bthe code editor.


The appearance of the program interface

At the top, shortcuts to open files with program texts are displayed (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 name other than 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 probable error, but this does not always coincide with 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 when it ends. 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 desired 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 indicated, 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.

We create the first program in PascalABC.NET. We analyze the main points.

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

Let's analyze the fastest algorithm for finding prime numbers. Sieve of Eratosthenes. An experiment to compare algorithms. Let's analyze a few more functions and features of the Pascal language ...

PascalABC.NET - SCHOOL - Compilers (1.Introduction) - Allocation of numbers

We begin to understand compilers. We are looking for integers 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. Learn to highlight words and lines.

PascalABC.NET - SCHOOL - Solving the problem for grade 7

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

Writing a powerful calculator that parses parentheses and functions like sin or trunc. OOP (Object Oriented Programming).

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

CONTINUED !!! Finishing up with a powerful calculator that parses parentheses and functions like sin or trunc. OOP (Object Oriented Programming).

PascalABC.NET - SCHOOL - 1. Advanced Phone Book! Quick search. Indexing

How to implement fast search in large amounts of data. Graph theory. We create on the example of the phone book.

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 make it possible 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
Integer Standard Functions 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 transactions 46
Standard functions 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 (Boolean) 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
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 the 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
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 Transfer Methods 181
Conclusions 183
Test questions 184
TOPIC 12. Files: save the results of 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 a two-dimensional array of numbers in a text file 192
Saving Numerical Data in a Text File 192
Saving an array of numbers to 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 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 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
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. 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
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
Graphic arts. Straight 303
Graphic arts. 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 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.