Quantities: constants, variables, types of quantities. Assignment, input and output of values. Linear algorithms for working with quantities. Types of quantities

Algorithms for working with quantities

Computer as an executor of algorithms

You already know that every algorithm is compiled for a specific performer. Now, as a performer, we will consider a computer equipped with a programming system in a specific language.

The executing computer works with certain data according to specific program. Data is a set of quantities.

Quantities: constants and variables

A computer works with information stored in its memory. A separate information object (number, symbol, string, table, etc.) is called a value.

Every value processed by the program takes its place (field) in the computer’s memory. The value of a quantity is the information stored in this memory field.

Exist three main types of quantities, with which the computer works: numeric, symbolic And logical . Studying databases and spreadsheets, you've already met these types. In this chapter we will build algorithms that work with numerical values.

Numerical quantities in programming, as well as mathematical quantities, are divided into variables and constants (constants). For example, in the formula (a 2 - 2ab + b 2) a, b are variables, 2 is a constant.

Constants are written in algorithms with their decimal values, for example: 23, 3.5, 34. The value of the constant is stored in a memory cell allocated for it and remains unchanged during program operation.

Variables in programming, as in mathematics, they are denoted by symbolic names. These names are called identifiers(from the verb “identify”, which means “to designate”, “to symbolize”). An identifier can be one letter, many letters, a combination of letters and numbers, etc. Examples of identifiers: A, X, ВЗ, prim, r25, etc.

Command system

You know that every algorithm is built based on the system of commands of the performer for whom it is intended.

Regardless of what programming language the program is written in, the algorithm for working with quantities is composed of following commands:

- assignment ;

- input ;

- conclusion ;

- appeal to auxiliary algorithm ;

- cycle ;

- branching .

Assignment command

The assignment command is one of the main commands in algorithms for working with quantities. We will write it like this:

The ":=" icon reads "assign". For example:

The computer first evaluates the expression, then assigns the result to the variable to the left of the ":=" sign.

If before executing this command the contents of the cells corresponding to the variables X, Y, Z were as follows:

A dash in cell Z means that the starting number in it can be any. It has no bearing on the outcome of this command.

If there is a numeric variable to the left of the assignment sign, and to the right - mathematical expression, then such a command is called arithmetic assignment instruction , and the expression is arithmetic.

In a particular case, an arithmetic expression can be represented by one variable or one constant. For example:

Input command

The values ​​of the variables that are the initial data of the problem being solved are usually specified by input.

The input command in the algorithm descriptions will look like this:

For example:

input A, B, C

On modern computers input is most often performed in dialogue mode with the user. Upon input command, the computer interrupts the execution of the program and waits for user action. The user must type the input variable values ​​on the keyboard and press a key. The values ​​entered will be assigned to the corresponding variables in the input list and program execution will continue.

Here is the execution diagram for the above command.

1. Memory before command execution:

5. The processor proceeds to execute the next program command.

When performing step 3, the entered numbers must be separated from each other by some delimiters. Usually these are spaces.

From the above we can conclude:

Value variables receive specific values ​​as a result of executing an assignment or input command.

If a variable is not assigned any value (or is not entered), then it is undefined. In other words, nothing can be said about what value this variable has.

Output command

The results of solving a problem are communicated by the computer to the user by executing an output command.

Output command in the algorithms it will be written like this:

For example:

output X1, X2

With this command, the values ​​of the variables X1 and X2 will be displayed on the output device (most often this is the screen).

You will learn about other commands used in computational algorithms later.

Questions and tasks

1. What is quantity? What is the difference between variable and constant quantities?

2. How is the value of a quantity determined?

3. What are the main types of quantities in programming?

4. How is the assignment command written?

5. What is input? How is the input command written?

6. What is a conclusion? How is the output command written?

7. In a schematic form (as done in the paragraph), reflect the changes in values ​​in the cells corresponding to variables A and B during the sequential execution of assignment commands:

1) A:=1
B:=2
A:=A+B
B:= 2xA
2) A:=1
B:=2
C:=A
A:=B
3) A:=1
B:=2
A:=A+B
B:=A-B
A:=A-B

8. Instead of an ellipsis, enter several assignment commands into the algorithm, which should result in an algorithm for raising the entered number to the 4th power (do not use additional variables except A).

In the Pascal language, any value belongs to some type.

The type of the quantity determines the range acceptable values, accepted by quantities of this type; a set of operations allowed on a given value and the amount of memory allocated for this value.

Variable –

for a computer, this is a named memory location;

For a programmer, it is a quantity that has a name, a type, and a value.


The variable receives its name and type in the descriptions section, and its value in the operators section.

Example of variable description: Var D, C, N: Integer; LogPer: Boolean;

Assignment operator serves to change the value of a variable and has the following form:

Sign « := » called "appropriation".

Note: The expression to the right of the assignment sign must be of the same type as the variable to the left of the assignment sign.

For example:


Exercise 1: Vasya walked from home to school (1.5 km) in 20 minutes. Create an algorithm for finding the speed of movement in m/s. (block diagram and program)

Task: change the algorithm to find Vasya's speed in km/h

Homework questions and tasks:

  1. What determines the type of a quantity? Tell us the types, give examples.
  2. How do the programmer and the computer perceive the variable?
  3. When and how is the name and type of a variable determined? Give examples.
  4. What is the purpose of the assignment operator and how does it work? Examples.

Tasks:

№ 5. Write the following numbers according to the rules for writing numbers in the Pascal language:

0,00237*10 5 -125,87 +79 87*10 10 10 3 257 200 000 0,1 0,4*10 -5 -10 -10

№ 6. Which of the following numbers define the same value:


h. 0.00000351E+8

And. 0.00000351E7

№ 7. Manually scroll through the program and determine the values ​​of the variables

№ 8. Variables A and B contain integer numbers, for example, A:=20; B:=13. Swap the numbers in these variables. Be careful! If you write A:=B, then you will lose the number 20. Use the third variable - C (see figure)

№9 . Masha, moving at a speed of 0.8 m/s, walked from the store to the house in 15 minutes. Find the distance from the store to the house. Create a flowchart and program.


It is known that every algorithm is compiled for a specific performer. As a performer we will consider a computer equipped with a programming system in a specific language. The executing computer works with certain data according to specific system commands
Data. A computer works with information stored in its memory. A separate information object (number, symbol, string, table, etc.) is called a value. The quantities processed by the program are called data.

Quantities in programming, just like mathematical quantities, are divided into variables and constants (constants).

For example, in the formula (a 2 -2ab+b 2) a, b - variables, 2 - constant.

Constants are data that are fixed in the text of the program and do not change during its execution.

Constants are written in algorithms with their decimal values, for example: 23, 3.5, 34. The value of a constant is stored in a memory cell allocated for it and remains unchanged during program operation.

Variables in programming, as in mathematics, are designated by symbolic names and can change their values ​​during program execution. Names are called identifiers (from the verb “identify”, which means to designate, symbolize). The identifier can be one letter, many letters, or a combination of letters and numbers. As a rule, only letters of the Latin alphabet are used and the first character in the identifier is a letter. Examples of identifiers: A, X, BS.prim, r25, etc.

Variables can be integer, real, logical, character and literal.

There are three main types of quantities that a computer works with:

  • numerical– whole and real numbers,
  • symbolic– text that may contain letters (Russian and Latin), numbers, punctuation marks, service symbols, etc.,
  • logical– takes two values: True (true) and False (false).

Command system. Any algorithm is built based on the system of commands of the performer for whom it is intended. Regardless of what programming language the program is written in, the algorithm for working with quantities is composed of the following commands: assignment; input ; conclusion.

Solution process computational problem- it's a process consistent change variable values. As a result, a result is obtained in certain variables. A variable receives a specific value as a result of an assignment. The assignment command is one of the main commands in algorithms for working with quantities. Assignment is entering into a cell allocated for a variable, certain value as a result of command execution.

To set the value of a variable, use the assignment operator. When an assignment statement is executed, the variable whose name is to the left of the equal sign is given a value equal to the value of the expression (arithmetic, string, or Boolean) that is to the right of the equal sign.

We will write it like this:
<переменная> := < выражение>
The ":=" icon reads "assign". For example: Z=X+Y.
The computer first evaluates the expression, then assigns the result to the variable to the left of the “=” sign.

If before executing this command the contents of the cells corresponding to the variables X, Y, Z were as follows:

The values ​​of the variables that are the initial data of the problem being solved are usually specified by input.
The input command in the algorithm descriptions will look like this:

input <список переменных>.
For example, in Pascal: Readln(A, B, C)

If a variable is not assigned any value (or is not entered), then it is undefined.

The results of solving the problem are communicated by the computer to the user by executing the output command:

conclusion <список переменных>.
For example, Pascal: Writeln(A, B, C)

Expressions - intended to perform the necessary calculations, consist of constants, variables, function pointers (for example, exp(x)), united by operation signs.

Expressions are written as linear sequences of characters (without subscripts or superscript characters, “multi-story” fractions, etc.), which allows you to enter them into the computer by sequentially pressing the corresponding keyboard keys.

There are arithmetic, logical and string expressions.

Types of operations:

arithmetic operations+ , - , * , / etc. ;

logical operations and, or, not;

relation operations< , > , <=, >= , = , <> ;

operation couplings(otherwise, “attachment”, “concatenation”) of symbolic values ​​with each other to form one long line; is represented by a "+" sign.

Operators (teams). An operator is the largest and most meaningful concept of a language: each operator is a complete phrase of the language and defines some completely completed stage of data processing. The operators include:

Operators are divided into executable and non-executable. Non-executable statements are intended to describe data and program structure, while executable statements are used to perform various actions (for example, the assignment operator, input and output statements, conditional operator, loop operators, procedure operator, etc.).

Linear algorithmic structure

To represent the algorithm in the form, computer understandable, are programming languages. First, an action algorithm is developed, and then it is written in one of these languages. The result is a program text - complete, complete and detailed description algorithm in a programming language. Exists a large number of algorithms in which commands must be executed one after another. Such algorithms are called linear.

A program has a linear structure if all statements (commands) are executed sequentially one after another.

Example: program in language Pascal programming, adding two numbers

Readln(a, b);
c:= a + b;
write("Result =", c);

1. Quantities: constants, variables, types of quantities. Assignment, input and output of values. Linear algorithms working with quantities.

2. Searching for information on the Internet using query language.

1. Quantities: constants, variables, types of quantities. Assignment, input and output of values. Linear algorithms for working with quantities.

It is known that every algorithm is compiled for a specific performer. As a performer we will consider a computer equipped with a programming system in a specific language. The executor computer works with certain data according to a certain command system.

Data. The computer works with information stored in its memory. A separate information object (number, symbol, string, table, etc.) is called size . The quantities processed by the program are called data.

Quantities in programming, just like mathematical quantities, are divided into variables And constants(permanent).

For example, in the formula (a 2 -2ab+b 2) a, b - variables, 2 - constant.

Constants- this is data that is recorded in the text of the program and does not change during its execution.

Constants are written in algorithms with their decimal values, for example: 23, 3.5, 34. The value of a constant is stored in a memory cell allocated for it and remains unchanged during program operation.

Variables in programming, as in mathematics, they are designated by symbolic names and can change their meanings during program execution. Names are called identifiers (from the verb “identify”, which means to designate, symbolize). The identifier can be one letter, many letters, or a combination of letters and numbers. As a rule, only letters of the Latin alphabet are used and the first character in the identifier is a letter. Examples of identifiers: A, X, BS.prim, r25 and so on.

There are variables integer, real, logical, character and literal.

Exist three main types of quantities, With with which the computer works:

  • numerical – integers and real numbers ,
  • symbolic – text that may contain letters (Russian and Latin), numbers, punctuation marks, service symbols, etc.,
  • logical – takes two values: True (true) and False (false).

Command system. Any algorithm is built based on the system of commands of the performer for whom it is intended. Regardless of what programming language the program is written in, the algorithm for working with quantities is composed of the following commands:

- assignment- input Output

The process of solving a computational problem is a process of sequentially changing the values ​​of variables. As a result, a result is obtained in certain variables. A variable receives a specific value as a result of an assignment. Assignment command- one of the main commands in algorithms for working with quantities. Assignment is the entry of a certain value into a cell allocated for a variable as a result of executing a command.

To set the value of a variable, use assignment operator. When an assignment statement is executed, the variable whose name is to the left of the equal sign is given a value equal to the value of the expression (arithmetic, string, or Boolean) that is to the right of the equal sign.

We will write it like this:<переменная> = < выражение>The "=" sign reads "assign". For example: Z=X+Y.

The computer first evaluates the expression, then assigns the result to the variable to the left of the “=” sign.

If, before executing this command, the contents of the cells corresponding to the X variables Y, Z, it was like this:

then after executing the command it will become like this:

Values ​​of initial variables The data of the problem being solved are usually specified by input. Input command in the descriptions of the algorithms it will look like this:

input <список переменных>. For example, in BASIC: INPUT “Enter variables ”; A, B, C. If a variable is not assigned any value (or is not entered), then it is undefined.

The results of solving the problem are communicated by the computer to the user by executing output commands:

conclusion <список переменных>. For example, in BASIC: PRINT X1, X2.

Expressions - are intended to perform the necessary calculations, consist of constants, variables, function pointers (for example, exp(x)), united by operation signs.

Expressions are written in the form linear sequences of characters(without subscript and superscript symbols, “multi-story” fractions, etc.), which allows you to enter them into the computer by sequentially pressing the corresponding keyboard keys.

There are expressions arithmetic, logical and string.

Types of operations:

  • arithmetic operations +, -, *, /, etc.;
  • brain teaser operations and, or, not;
  • operations relationship< , > , <=, >= , = , <> ;
  • operation couplings(aka, "attachment", "concatenation") of character values ​​with each other to form one long string; is represented by a "+" sign.

Operators(teams). An operator is the largest and most meaningful concept of a language: each operator is a complete phrase of the language and defines some completely completed stage of data processing. The operators include:

  • keywords;
  • data;
  • expressions, etc.

Operators are divided into executable and non-executable. Non-executable statements are intended to describe data and program structure, and executable- to perform various actions (for example, assignment operator, input and output operators, conditional operator, loop operators, procedure operator, etc.).

Linear algorithmic structure

Programming languages ​​are used to present the algorithm in a form understandable to a computer. First, an action algorithm is developed, and then it is written in one of these languages. The result is a program text - a complete, complete and detailed description of the algorithm in a programming language. There are a large number of algorithms in which commands must be executed one after another. Such algorithms are called linear.

A program has a linear structure if all statements (commands) are executed sequentially one after another.

Example: QBasic program that adds two numbers

Input a, b c = a + b

print “Result =”, c

Topic 6. Quantities, their types and properties. Lazarus environment.

I. Computer science, mathematics, physics and other sciences use quantities.

Magnitude is a quantitative expression of the value of a property of an object (Wikipedia).

Examples of quantities in computer science are:

Examples of quantities in mathematics are:

Examples of quantities in physics:

Quantities are divided into variables and constants (constants).

1. A quantity whose value does not change is called constant, or constant.

An example of a mathematical constant is, for example, the number π

2. The value that is different moments time can take on different values, called a variable.

II. All actions in programming are performed on certain quantities (data).

The variable represents a reserved space in random access memory(one or more cells) for temporary storage of data. The address of the entire section is the address of the first cell.

The variable is characterized by the following properties:

To make your variables more visual and easier to read, it is recommended to give them names that have a specific meaning. There are several rules for naming variables:

The type of a variable determines what values ​​this variable can take, what operations can be performed on it, and what size memory the program will allocate to store the value of this variable.

In Lazarus, each variable must be declared (declared) before use. When declaring a variable, its name and type are specified. Space is allocated in RAM to store a variable. The types of variables used in the procedure are indicated after keyword var (English variable - variable) in the line that is located between the procedure title line and the line with the word begin. This line is called the variable declaration line. To describe variables, the service word var is used.

To describe constants in the Object Pascal programming language, the service word Const is used.

Above the text of the compiled procedures is another line var. This line specifies the name of one Forml variable of type TForm.

III. Numerical quantities

Integer data type

Integer data types can occupy one, two, or four bytes in computer memory.

To convert the text representation of an integer into the integer itself, the StrToInt function (English string to integer) is used, and for the reverse conversion, the IntToStr function is used.

Real Data TypeThe internal representation of a real number in computer memory is different from the representation of an integer. Real number represented in exponential form mE^p, where:

To move from exponential form to the usual representation of a number, you need to multiply the mantissa by ten to the power (order). A real number can take from 4 to 10 bytes.

Operations and Expressions

The expression specifies the order in which actions are performed on the data and consists of operands (constants, variables, function calls), parentheses and signs of operations. For example: A + B * sin (x). The table presents the main algebraic operators of the Object Pascal programming language

Standard functions are defined in the language standard features. Some arithmetic functions are presented in the table below.

Conversion functionsType conversion functions are often used when inputting and outputting information. For example, in order to display the value of the Real variable in the output field (Label component) of a dialog box, you need to convert the number into a string of characters representing given number. This can be done using the FloatToStr function, which returns a string representation of the value of the expression specified as a parameter to the function.

Basic type conversion functions

An example of using type conversion in a project.

Let's create such a procedure TForml.ButtonlClick - handler OnClick events, the execution of which makes it possible to set an arbitrary window color and increase its height by an arbitrary whole number of pixels, change the position of the button relative to the top and left borders of the form.

To do this you need:

2. Change value Text properties of these fields to '' (empty text).

3. Place an inscription to the left of each field, change the value of the Caption inscription property:

4. Place a button on the form in the lower right corner and change the value of its Caption property to Edit.

Quantities text type, operations on them

Linear algorithms