Is the assignment operator written correctly in Pascal. assignment operator. Input, output, assignment operators

Lesson type:

  • educational. Students are introduced to a new Pascal language operator;
  • repetition of the theme Inner memory computer";
  • reinforcing acquired knowledge.

Lesson topic:“Programming in Pascal. assignment operator.

Goals:

  • Introduce the concept of "Assignment Operator".
  • Distinguish operations valid for integers and real numbers.
  • Correctly compose and evaluate expressions using the assignment operator.
  • Write programs for simple tasks.

Lesson duration: 1 hour 30 minutes.

DURING THE CLASSES

First lesson

1. Repetition of the topic of the previous lesson "Data Types". Examples of using words that sound the same but have different meanings.

The teacher and students work in the form of "question-answer" (10 minutes).

2. The teacher explains the new topic using the multimedia lesson-presentation “Programming in Pascal. assignment operator. The definition of the concept of "assignment operator" is given, the general and structural form of the operator is given. Before considering the work of the assignment operator, we recall and repeat the topic “Random Access Memory, Properties Random access memory».

Accompanied by a presentation. Appendix 1 (15 minutes).

3. Students work independently, reading and taking notes on a new topic (lesson-presentation is pre-recorded on students' computers, questions for note-taking are determined by the teacher). Here, students can get acquainted with the texts of programs that use the assignment operator. Annex 2(20 minutes).

Second lesson

4. Students work at the blackboard, doing exercises (12-15 minutes).
1. The teacher conducts knowledge control in the form of a test. The test program is pre-recorded on the students' computers. The score is set by the program. Annex 5(10 minutes).
5. Students do a little independent work on the cards, or write and debug a program in the LPL Pascal, which uses the assignment operator (s) (the teacher calls the numbers of the cards with the tasks). Students are graded for their work. Annex 3 . Appendix 4(15 minutes).
Homework. Students are invited to write the text of a simple program in the LPL Pascal, which uses the assignment operator (s) (for those students who did not have time to complete the task in class).

Findings:

  • Through block 1 repetition of the topic of the previous lesson.
  • Through blocks 2, 3 and 4, a new topic “Assignment Operator” is given and fixed.
  • Through blocks 5 and 6, knowledge control is performed.

Programming in Pascal

Lesson #1 (2 hours). Variables and constants. assignment operator

  1. Checking homework.
  2. New topic.
  3. Homework.

Checking homework.

2. New topic: “Variables and constants. Assignment Operator"

Variable - is the name of the memory area (the size of the area depends on the data type) that stores the value assigned to the variable. A variable can be compared to a box that contains a value (the best comparison is to an apartment building, since you can take something out of the box and it will be the last one, i.e. the box remains empty, and the variable, even if it was used, remains in the memory area).
Constant - fixed single numeric or text values ​​that cannot be changed during program execution.
assignment operator. One of the most used operators assignment operator. General view of the assignment operator:

variable name:= expression;

The expression can be both a specific value (i.e. an integer or real number, for example, 22 or 2.5, a symbolic value: "ABC", a logical value: TRUE or FALSE), and some expression (values ​​+ operations on them), the result of which will be assigned to the variable on the left side.

EXAMPLES:

PAG:= 10;(specific value as a number);
X:= X + 1;(expression);
C:=B;(variable name);
G:= 'HELLO'; ( specific symbolic value);
X:= (-A + SIN(Y) - 4 * B * X) / (2 * C);(mathematical expression);
Y:= TRUE;(specific logical value).

The difference between the assignment operation in mathematics and programming.

In mathematics, the expression = in means that the computed values ​​for a and b must be equal.
In programming, A := B is understood to mean that a value stored in a memory area named B is placed in a memory area named A. Therefore, the inverse expression
B:= A gives a different result.
Example: The values ​​5 and 9 are placed in A and B, respectively.

A: = 5;
B:=9

a) The result of operation A:= B;

b) The result of operation B:= A;

Here is another significant difference between the equals sign and the assignment operator: A = A + 1;
In mathematics, such an expression is simply meaningless, since the identity is false. There are no such numerical values ​​of A for which it would be true. In programming, this means that 1 is added to the value stored in cell A, and the newly obtained value replaces the old one in cell A. For example, if A had a value of 5 before executing this statement, then after execution it will be 6.

The next difference: the mathematical expression A + 9 - B = X is quite reasonable and can mean that when you substitute some numbers instead of A and B in the left side of the expression, you will get N. At the same time, in programming this expression is meaningless, since the left side of the expression must contain the name of the memory area to which the value computed on the right side is assigned. If you try to insert such an expression into your program, you will get an error message, because the variable name is A + 9 - B will not understand.

write in the usual form:

(- b + sqrt(sqr(b) - 4 * a * c))/(2 * a);
a / b * (c + d) - (a - b) / b / c + 1E - 8;

Bibliography:

  • V.B. Popov"TURBO PASCAL for schoolchildren I", "Finance and statistics", Moscow, 1996
  • A.A. Cherno in "Synopsis of computer science lessons in grades 9 - 11", "Teacher", Volgograd, 2004.
  • D.M. Zlatopolsky“I am going to a computer science class. Problems in programming, grades 7 - 11", "First of September", Moscow, 2001.

Operator- an order to perform specific actions. The Pascal programming language contains 11 operators.

Assignment operator in Pascal

The assignment operator is the most commonly used operator in Turbo Pascal.

Record format: X:=A;
where: X– variable name;
A- expression ;
:= - assignment sign.

Operator work: During the execution of the statement, the value on the right side of the expression is calculated and assigned to the variable name.

The type of the variable and the type of the expression must match, except when the expression is of an integer type and the variable is of a real type. In this case, the calculated value of the expression is converted to a real type.

Example 9.1. Write a program for calculating functions:

y=cosa+inb; n=k+1; r=ord(#65).

Var
a, b, y: real;
k, n: integer;
r: char;
t, v, u: Boolean;
* * *
y:= cos(a)+ln(b);
n:=k+1;
r:=ord(#65);
t:=(v and u)or(v and not u);
* * *
end.

Jump operator in Pascal

In Turbo Pascal, the natural order of execution of operators is adopted, i.e. in the order in which they appear in the text of the program. The jump statement interrupts the natural order of execution of statements and transfers control to the statement marked with the label specified in the jump statement.

Recording format: GoTo m;

where: goto- go to;
m- label.

Operator work: The jump statement transfers control to the statement marked with the label m.
The use of a jump statement in a TR is undesirable because it violates the structural integrity of the program. Such a program is difficult to read, debug, and modify. The use of the jump operator in the TR is subject to restrictions that prevent its indiscriminate use.

Using the jump statement, you can transfer control within one level or jump from an internal level to an external one. Transitions from the outer layer to the inner layer are prohibited. For example, from a program to the body of a subroutine or inside a loop statement or a conditional statement.


The assignment operator is the simplest and most common operator.

Assignment operator format

An assignment operator is a notation that contains the symbol = (equals sign), to the left of which is the name of the variable, and to the right - the expression. The assignment operator ends with a sign; (semicolon).

Variable = Expression ; // assignment operator

The assignment operator can be distinguished in the text of the program by the presence of an equal sign. An expression can be a constant, a variable, a function call, or the expression itself.

Assignment operator execution rule

The assignment operator, like any other operator, is executable. This means that the entry that makes up the assignment statement is executed according to the rule. When an assignment operator is executed, the value on the right side is evaluated, and then this value is assigned to the variable to the left of the equals sign. As a result of the execution of the assignment statement, the variable on the left side always gets a new value; this value may be different or the same as the previous value of this variable. The expression on the right side of the assignment operator is evaluated in accordance with the order in which expressions are evaluated (see Operations and Expressions).

Assignment Statement Examples

In an assignment operator, a variable type declaration is allowed to the left of the equals sign:

int In = 3 ;
double Do = 2.0 ;
bool Bo = true ;
color Co = 0 x008000 ; // Variable Co is assigned a constant value
stringSt = "sss"; // Variable St is assigned the value of a constant
datetime Da = D "01/01/2004"; // Variable Da is assigned the value of a constant

Previously declared variables are used in an assignment statement without specifying a type.

In = 7 ; // Variable In is assigned the value of a constant
Do = 23.5 ; // Variable Do is assigned the value of a constant
Bo = 0 ; // Variable Bo is assigned the value of a constant

In an assignment operator, it is not allowed to declare the type of a variable on the right side of the equal sign:

In = int In_2; // Declaring the type of variables on the right side is prohibited
Do = double Do_2; // Declaring the type of variables on the right side is prohibited

The assignment statement does not allow the repeated declaration of the type of variables.

int in; // Variable type declaration In
int In = In_2; // Re-declaration of a variable is not allowed (In)

Usage examples on the right side of custom and standard functions:

In = My_Function(); // In is set to user. functions
Do = Gipo (Do1 , Do1 ) ; // Do set to user. functions
Bo = IsConnected(); // Bo set to std. functions
St = ObjectName(0) ; // St is set to std. functions
Da = TimeCurrent(); // Da is set to std. functions

Examples of use on the right side of expressions:

In = (My_Function() + In2 ) / 2 ; // Variable In is assigned
// ..expression value
Do = MathAbs (Do1 + Gipo (Do2 , 5 ) + 2.5 ) ; // Variable Do Is Assigned
// ..expression value

When calculating in an assignment operator, the rules for casting data types are applied (see Type Casting).

Short Form Assignment Examples

MQL4 also uses a short form of assignment operators. This is a form of assignment statements that uses assignment operations other than the assignment operation = (equal sign) (see Operations and Expressions). Short form operators are subject to the same rules and restrictions. The short form of assignment operators is used in the code for clarity. The programmer, at his discretion, can use one or another form of the assignment operator. Any short-form assignment operator can be easily rewritten into a full-format one, and the result of the operator's execution will not change.

In /= 33 ;
In = In / 33 ;

St += "_exp7"; // Short form of the assignment operator
St = St + "_exp7"; // The full form of the assignment operator

We have considered the simplest programs written in Pascal in the PascalABC.Net environment. There we performed arithmetic operations on numbers, and then output them using the write (or writeln) operator. But we did not use the assignment operator, which is very common in programming. So what is assignment?

Consider some variables M and N that have certain values, say, M = 12, N = 34. For each of these values, the computer allocates a separate memory area, which is a certain number of cells, or bits (1 cell = 1 bit). Multiple numbers (or other type of data) cannot be stored in the same memory location for the same reason that two liters of water cannot fit in one liter jar.

Very often it becomes necessary to remove the value of the variable M, equal to 12, and instead become equal to the value of the variable N, that is, 34. What does the computer do? It finds the memory location where the value of N is “hidden”, copies it (the number 34), returns to the storage location M (equal to 12), deletes its value and inserts a new one, that is, 34. As a result, M = 12 turned into M = 34.

Assignment is the recording of data in the area of ​​computer memory allocated for the value of M, the data that is stored in another area of ​​computer memory, where the value of N is recorded.

Assignment is written as M:= N , meaning that the value of N goes to M. The natural question arises: what happened to the value of N? Nothing, she just “shared” the value with the variable M, but she herself did not change. Sometimes newcomers to programming think that the variables M and N are swapped, but they are not. To illustrate this phenomenon, consider the problem:

A task. Numbers are given: M = 12, N = 34. Swap their values.

We need to assign 34 to the number M, and 12 to the number N (vice versa). Very often, novice programmers write like this:


In the first line of the code, M is assigned to N, that is, M:= 34. In the second line, N is assigned to M, but the last variable is no longer equal to 12, since it changed its value in the previous line to 34. Therefore, both variables will be equal to 34. For demonstration we will write the complete code that you must rewrite in the PascalABC.Net programming environment:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 var M, N: type integer;

Description:

")" onmouseout="toolTip()">integer
; begin writeln ( "Enter numbers M and N:" <-- вводим 12 } write (" N = " ) ; readln (N) ; { <-- вводим 34 } M := N; { <== M присваивается N, поэтому M = 34 } N := M; { <== N присваивается M, поэтому N = 34 } end.

Run the program for execution; enter 12 when prompted for M, enter 34 for N, and press Enter. If everything is done correctly, you should see:

Enter numbers M and N:
M=12
N=34
Result:
M=34 N=34

As you can see from the last line, this is not what we wanted - to swap the values ​​of M and N in places.

To achieve the correct result, it is necessary to fix the initial value of the variable M, for example, s:= M ("remember" M by writing its value to the variable s). As a result, we will assign to the number N not the value of the variable M, which has already changed, but use the value s equal to it:


In accordance with the latest changes, the previous program will take the form:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var M, N, s: type integer;

Description:
Represents a 32-bit signed integer.

Value range: -2 147 483 648 .. 2 147 483 647")" onmouseout="toolTip()">integer
; begin writeln ( "Enter numbers M and N:") ; write("M="); readln(M); (<-- вводим 12 } write (" N = " ) ; readln (N) ; { <-- вводим 34 } s := M; { <== запоминаем M, равное 12 } M := N; ( M = 34 because N = 34 ) N := s; ( N = 12 since s = 12 ) writeln("Result:" ) ; writeln (" M = " , M, " N = " , N) ; readln end.

Rewrite this program in PascalABC.Net and run (F9 key on the keyboard). If the answer is:

Enter numbers M and N:
M=12
N=34
Result:
M=34 N=12

then the program works correctly. As you can see from the last line, the values ​​of the numbers M and N are swapped.

Assignment- a binding mechanism in programming that allows you to dynamically change the links between the names of data objects (usually variables) and their values. Strictly speaking, changing values ​​is a side effect of the assignment operation, and in many modern programming languages, the operation itself also returns some result (usually a copy of the assigned value). At the physical level, the result of an assignment operation is to write and rewrite memory cells or processor registers.

Assignment is one of the central constructs in imperative programming languages, implemented efficiently and simply on the von Neumann architecture that is the basis of modern computers.

In object-oriented programming languages, the semantics of assignment are quite different. For example, in the Kotlin language, when assigning, the object is copied, and in the Rust language, the object is moved (move-semantics) and the old bundle becomes invalid.

set<целевая_переменная> <выражение>

This notation is equivalent to calling a function. Similarly, in old-style COBOL:

MULTIPLY 2 BY 2 GIVING FOUR.

Work algorithm

  • Calculate the left-hand value of the first operand. At this stage, the location of the target object, the receiver of the new value, becomes known.
  • Calculate the right hand value of the second operand. This step can be arbitrarily large and include other statements (including assignments).
  • Assign the computed right hand value to the left hand value. First, in case of a type conflict, they must be cast (or an error message is issued due to its impossibility). Secondly, the actual assignment of values ​​in modern programming languages ​​can be substituted and include not only the transfer of values ​​of memory cells (for example, to "properties" of objects in , operator overloading).
  • Return the computed right-hand value as the result of the operation. Not required in all languages ​​(for example, not needed in Pascal).

Designation

The choice of the assignment symbol is a matter of controversy among language designers. There is an opinion that the use of the symbol = for assignment confuses programmers, and also raises the question of choosing a symbol for the comparison operator, which is difficult to solve well.

A well-known bad example is the choice of an equal sign to denote an assignment, which dates back to Fortran in 1957 and is still blindly repeated by a mass of language developers. This bad idea overthrows the age-old tradition of using the "=" sign to denote an equality comparison, a predicate that evaluates to "true" or "false". But in Fortran, this symbol began to denote assignment, coercion to equality. In this case, the operands are in an unequal position: the left operand, the variable, must be made equal to the right operand, the expression. So x = y does not mean the same thing as y = x.

Original text (English)

A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957 and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand (a variable) is to be made equal to the right operand (an expression). x = y does not mean the same thing as y = x.

The implementation of this position of Wirth can be considered that in the Pascal language, of which he is the author, the assignment operator is: = , while for comparison, simply = is used.

The choice of the equality operator character in the language when using = as an assignment is resolved:

  • By introducing a new language symbol for the equality test operator.
A = B = C

the variable A is assigned the Boolean value of the relation expression B = C. This notation leads to a decrease in readability and is rarely used.

Semantic features

Far from always "intuitive" (for programmers of imperative languages) way of interpreting the assignment is the only true and possible one.

From the syntax used in imperative languages, it is not always possible to understand how the assignment semantics are implemented unless it is explicitly defined in the language.

A = b = a a = 1000

After that, b will have a value - simply because, in fact, its value is the value of a . The number of references to the same data object is called its cardinality, and the object itself is killed (destroyed or given to the garbage collector) when its cardinality reaches zero. Lower-level programming languages ​​(such as C) allow the programmer to explicitly control whether pointer semantics or copy semantics are used.

Operation substitution

Many languages ​​provide the ability to change the meaning of an assignment, either through the property mechanism or through overloading the assignment operator. Substitution may be needed to perform checks on the validity of the assigned value or any other additional operations. Overloading of the assignment operator is often used to provide a "deep copy", that is, copying values ​​rather than references, which are copied by default in many languages.

Such mechanisms make it possible to provide convenience at work, so for a programmer there is no difference between using a built-in operator and an overloaded one. For the same reason, problems are possible, since the actions of the overloaded operator can be completely different from the actions of the default operator, and the function call is not obvious and can easily be mistaken for a built-in operation.

Extended designs

Assignment constructs in various programming languages

Since the assignment operator is widely used, programming language developers are trying to develop new constructs to simplify the writing of typical operations (to add the so-called "syntactic sugar" to the language). In addition, in low-level programming languages, the inclusion criterion is often the ability to compile into efficient executable code. The C language is especially famous for this property.

Multiple targets

One alternative to the simple operator is the ability to assign the value of an expression to multiple objects. For example, in PL/1, the operator

SUM, TOTAL = 0

simultaneously assigns zero to SUM and TOTAL . In Ada, assignment is also a statement, not an expression, so the notation for multiple assignment is:

SUM, TOTAL: Integer:= 0;

A similar assignment in Python has the following syntax:

Sum = total = 0

Unlike PL/1, Ada, and Python, where multiple assignment is considered only a shorthand notation, in C, Lisp, and others, this syntax has a strict basis: the assignment operator simply returns the value assigned to it (see above). So the last example is actually:

Sum = (total = 0)

A line like this will work in C (if you add a semicolon at the end), but will cause an error in Python.

Parallel assignment

The last option only works with types that support bit operations (for example, for double, the language compiler will not allow you to exchange variable values ​​in this way).

a ||= 10

This construct assigns a value to the variable a only if the value has not yet been assigned or is equal to false .

Compound statements

The compound assignment operator allows you to abbreviate a commonly used form of assignment. Using this method, you can shorten the notation of an assignment that uses the target variable as the first operand on the right side of the expression, for example.