Rus Articles Journal

Lesson of programming. 1 part

the Beginning of the beginnings …

I so, we begin a cycle of the lessons devoted to programming on Delphi. Namely Borland Delphi 7. Today, an introductory lesson in which you learn bases of programming and you will create the simple calculator. So, we will begin!

Hard in the doctrine, it is easy in fight.

Actually the Delphi language does not exist, the fact that we call Delphi - it is the programming system on Pascal. Elements of Pascal are studied at school. All know how to write the program of addition of two numbers by means of the console. But there is always a wish bigger. And therefore many begin to learn language independently, by means of the self-instruction manual, however not all finish begun.

We will begin with the basic concepts which everyone who learns language has to know. Here the list, however, only the main definitions are given here:

A code - the text of the program which is written down in a programming language.

The source code - the Same, as a code.

The operator - Part of a code in which registers the actions necessary at work of the program.

Compilation - translation process of a program code in that which is clear to the computer, that is machine. For process of compilation use compilers.

The programming system - that in what is written by programs.

Keywords - the reserved words used in each programming language for creation of the program.

The announcement (data) - the description of data, for its further use.

Well, I think it so far will be enough to write the simple program. Of course I will explain each step.

First program.

Now we will begin to write the our first program, it will be the elementary calculator which is able to put to read, multiply and divide integers.

Having started the program you will see a window with a form and the editor of a source text, it is not necessary to us yet, we will pass to it on later. We should create the console application, for this purpose choose file - new - others - console application. you will see a source text. As the console has no visual components, we will resort to the standard words WriteLn and ReadLn, these words are intended for conclusion / input. Now we will begin to fill the prepared template.

After the word - we write key Var, it is intended for the announcement of variables. Also there is one more type of the declared data - it is constants. For example:

Now we will write down several variables, namely 3:a, b, c. they quite will be enough. All variables which it is necessary to declare write to a column after a keyword, and specify to what type it belongs.

Var a:integer;

b:real;

c:string;

In this example of a variable the type of an integer, b decimal, and from a line is appointed. It is necessary to us that all numbers were only whole, for this purpose we write:

Var a:integer;

b: integer;

c: integer;

We go further. Now we should make so that process of calculation of the sum of numbers began. For this purpose operators use. The operator begins the word Begin, and end comes to an end. But there is one subtlety the matter is that after the word end it is not always necessary to put an end. It is put only if the program comes to an end. end; - put after end of the next operator. After the word Begin signs do not put.

Now we will begin to write the most important - algorithm of the program. How it will calculate the sum of two numbers. It is of course banal arithmetics + the correct syntax, there is nothing difficult. After the word Begin we write with: =a+b;.

here is how it will look:

Var a:integer;

b:integer;

c:integer;

begin

with: =a+b;

Notice that end should not be put so far. Before line “with: =a+b;“, it is necessary to deliver a line “to ReadLn (a, b); “, it is necessary to enter data on these numbers into memory of the program. And “WriteLn(c);“, and it is necessary respectively for a conclusion of the received number. Well and in end of the text, we will put again ReadLn in order that the program was not closed ahead of time.

General view of all source code such:

program Project1;

{ $APPTYPE CONSOLE }

of uses

SysUtils;

Var a:integer;

b:integer;

c:integer;

begin

ReadLn (a, b);

WriteLn(c);

c: =a+b;

end.

The word { $APPTYPE CONSOLE } defines a type of the appendix, in this case it is the console. uses SysUtils; it is necessary for addition of additional modules, about that as to do it and we will talk about point later.

All! You wrote the first program. Enter two any integers through a gap and press “enter“, in line the answer will be output below.

Next time, we will make the calculator, in a form of a window Windows application, habitual for us. Let`s fill it with a context menu, and also several interesting functions. Also we will describe how to connect additional modules in the programs.