Last Updated:

Pascal Programming: Basic Concepts

Pascal is a programming language that is widely known all over the world today. It was created in the late sixties. The author of this project was a scientist originally from Switzerland. The programming language is used today by everyone who wants to learn competent and high-quality programming.

 

Pascal Programming: Basic Concepts

 

Preparations before the start

 

Let's say one day you decide to learn the basics of programming. What do you need to do that? Any expert who understands the case will say that without understanding pascal's language, there is no point in moving on. But first you will need to download the compiler by buying it.

What is such a program? This is a special variety with which you can read the program code. After that, with the help of this code, you can translate the received data into the language of machine codes. it turns out that one way or another programming languages are a tool for proper and high-quality interaction with the computer.

Fact! The compiler is a tool that helps make our everyday speech understandable to the computer.

There are several compilers that exist in relation to the Pascal programming language. There's Turbo Pascal, there's Free Pascal, there's Pascal ABC. Each of these programs has its own set of advantages. Everyone chooses for himself which option is the most convenient and comfortable in everyday activities. Pascal ABC is more suitable for those programmers who are just starting their journey. That is, this is the best choice for all beginners.

Starting to work at Pascal

So, run the compiler in the appropriate box. This is the basis for your further activities. You will see here your first programs and compose codes for them.

Fact! Before you get started, do not forget that the Pascal language is a combination of several key components at once.

These include syntax, that is, a collection of rules with requirements for writing language objects, the alphabet, as well as a list of requirements for using these objects, that is, semantics.

As part of the Pascal alphabet, you can see latin letters, which can be both lowercase and uppercase, numbers, as well as a special set of characters, or a combination of them. Semantics and syntax have a key task in the correct preparation of program sections. It is also necessary to accurately compile their structure. If you do not do this, the software may fail or not start at all.

What does the program look like in Pascal ABC?

In fact, it is characterized by a standard appearance. The window of the software environment is no different from the usual ones. It has a menu bar, after which you can see a toolbar that provides quick access to the main options. Following them is the so-called workspace. Here you can work with the codes and the corresponding editor.

Fact! In the upper pane, you can see shortcuts to open files where program texts are specified.

Creating programs becomes much more comfortable if you run several texts at the same time and interact with them. You can copy, or transfer a separate part of the code to another, opposite to it. If you switch to the program execution mode, an area will be opened in front of you where you can enter results or display any kind of data.

Operators that allow you to enter data

There are several types of them. It makes no sense to talk about the issue without discussing them. There are many statements such as write and writeln. The second is used in order to display information information with movement to a new line. Why? Thanks to this approach, you can give some meaning to the variable you are interested in. Let's give the following example for clarity:

program Programma1;
varA:integer;
begin
writeln('A='); read(A); {enter value and "attach" it to variable A}
write(A); {Display the value of variable A}
end.

You can see that you can use curly braces to describe in detail each action. This format is called a comment. To assign a value to the variable you specify, you can use the read statement. It is entered directly from the keyboard.

 

The example we have considered shows that during the execution of the statement you can notice the transformation of a line into a variable A. At the same time, the readln statement similar to it functions somewhat differently. When using it, you can enter the necessary line at the same time. It will be converted to a suitable variable for follow-up:

program Programma1;
varA:integer;
begin
readln(A);
write('A=', A); {line will look like this: "A=' entered value A' "}
end.

At this point, when you have an idea, albeit a small one, about the nature of data entry, you can talk about what this very information data can be. It is also necessary to understand the method of their processing.

Interaction with Pascal ABC

You have typed a certain text in the program. What to do after that? It is necessary, of course, to preserve it and assign it a certain name. To do this, you can run the file, then click the "save" command or use a special button on the toolbar. You will be offered a default name, but it does not fit, so you should choose something else. Type it with the keyboard to easily find it among other files in the future.

Fact! After the actions performed, it will be possible to run the program and see how accurately it functions.

To start the command, you need to go to the section with the program, and then click on the key, designed in the form of a green triangle. There is such a button on the toolbar. You can also run the Run command. You must compile the code before you run. What does that mean? If you find any errors in the program code, you will see the corresponding red line, where this very error will be described.

What are the cycles in Pascal's language? For, While, Repeat. Nested Pascal loops

 

Before moving on to this article, you should have already been familiar with the for loop and its operator. Let's briefly recall. A for loop is used when you need to repeat certain calculations multiple times. But what if you don't know exactly how many times you need to do it?

cycles in Pascal's language

while and repeat loops

 

And then two more operators with loops (in the future just loops) while and repeat rush to our aid. And if you need to put a condition at the beginning of the loop, then here we use the while, and if at the end - repeat. Well, now let's deal with the above.

While loop operator

The while loop has a fairly easy-to-understand structure: the while condition do operator

Let's move on to deciphering. This means that as long as the condition is true (it stands between while and do), the statement (after do) will be executed until the condition becomes false. It is important to understand that the condition check occurs before each new circle of the loop (new repeat), and under a false condition, the operator (the line after do) will not be executed for the last time - it is simply skipped.

But what to do if in the operator we need to write several lines of code (execute several statements at once)? To do this, use the begin-end statement brackets. It is important to note that when you use this method, a "semicolon (;)" is placed at the end of each statement. (after the latter, before END, not necessarily).

This is the structure that our loop will have using several operators:

while condition do
begin
operator 1;
operator 2;
operator 3;
. . . . . .
N
end operator;

Example 1. Suppose we want to find the sum of cubes of numbers from 1 to 10.

var
b, sum: integer;

begin
b := 1;
sum := 0; // initial amount
while b <= 10 do
begin
sum := sum + b*b*b;
inc(b)
end;
writeln('sum = ', sum); // display the result
readln
end.

At the beginning of the loop (line 7), you can see that it will be repeated until b < = 10. The body of the loop (operators) consists of lines in which, with each repetition, we add b3 (line 9) to the sum sum, and b, in turn, will increase by 1 (10 row). At the moment when b reaches the value of 10, the loop will perform its last calculation and will exit the loop, since our condition b < = 10 will have a false value (its value will be 11).

Example 2. Here is a well-known example where the function ex is located in the Taylor series. The calculations will be carried out with an accuracy of one millionth, and the result will be taken as S. We can notice that the initial value is a0 = 1, and the next terms are calculated by multiplying an by x and dividing by n.

The meaning of this code is simple: while all the members of the > 0.000001, the body of the loop will begin its work, where we calculate the amount specified by us. But if a member of the series is < = 0.000001, then the body of the loop stops working, exits and the result is displayed.

var a, x, S: real; n: integer
begin x := 0.5;
n := 0; // starting number
a := 1; // initial value of the row member
S := 0; // initial amount
while a > 0.0000001 do
begin
S := S + a;
inc(n);
a := a * x / n
end;
writeln('S =', S:0:6);
readln
end.

Try to avoid so-called "infinite loops", for example:

begin
while 1 < 100 do writeln(‘Hello!’) end.

Since our condition is a priori true (one can never be more than a hundred), the program will non-stop display the phrase "Hello". As you understand, in order to avoid such situations, you need to control that the condition is not constantly true. Display the smallest divisor of x other than 1

var x, d: integer;
begin write('Enter x --> ');
readln(x);
d := 2; { <- minimum divisor other than 1 }
while (x mod d <> 0) do inc(d);
writeln('d =', d);
readln
end.

Print a minimum number greater than 200 that is purposefully divisible by 17

var
n: integer

begin
n := 201; { <- minimum number greater than 200 }
while (n mod 17 <> 0) do inc(n);
writeln('Answer:', n);
readln
end.

Repeat loop operator

The repeat loop operator has a different structure from while:

repeat
оператор
until условие;

As you may have noticed, this operator is different in that the condition will be checked at the end of the loop. That is, if it is correct, there will be an exit from the cycle, and if it is false, the cycle will repeat. Because of this feature, this operator is also called an exit operator. There is one more difference: the start-end brackets are not required in the repeat loop, and the body of the cycle is written directly:

repeat
operator 1;
operator 2;
operator 3;
………..
operator N
until condition;

It is important to note that the operator(s) here will perform calculations at least once, because the condition is at the end (with the same false condition, the program would simply bypass the while loop). Using this, you can easily convert the while loop (with a precondition) into a repeat loop (with a postcondition), and vice versa - not always. But! The for loop statement can be converted to both while and repeat.

 

Calculate the square root from the number entered from the keyboard. When you start the program and enter negative numbers, you will constantly return to the beginning, since the "password" for exiting is only a positive number.

var
x: integer;

begin
repeat
readln(x)
until x >= 0; { <- exit if x>=0 }
writeln('Square root:', sqrt(x):0:4);
readln
end.

Enter numbers from the keyboard until their sum exceeds the number specified in advance. The exit from the loop will be made when the sum sum becomes > of the number M:

var
x, M, sum: real;

begin
write('Enter check number --> ');
readln(M);
sum := 0; { <- initial sum value }
repeat
readln(x); { <- enter x }
sum := sum + x { <- add x to the sum } until sum > M; { <== exit if sum exceeds M }
writeln('Result: ', sum);
readln
end.