What Is Assignment in Programming?
An operator is a special character that tells the translator that you want to perform an operation on some operands (for example, +,-,%,<<). Usually, programming languages define a set of operators similar to operators in mathematics: in a sense, operators are special functions. In addition to arithmetic operations, operators in programming can implement operations on logical values, string operations, etc. For example, comparison operators can be used to test the equality of two different values.
In contrast to functions, operators are the basic actions of a programming language. Their name is shorter and usually consists of special characters. Sometimes an operator is understood as an operation. Although it is more correct that the operator indicates which operation should be performed. The main types of operators include:
- Assignment operator;
- Selection operator (switch);
- Compound operator ({ } block operator);
- Break operator (break);
- Empty operator (;);
- Stepwise loop operator (for);
- Conditional operator (if else);
- Loop operator with a prerequisite (while);
- Loop operator with postcondition (do-while);
- Cycle continuation operator (continue);
- Function call operator;
- Unconditional transition operator (goto);
- Return from the function operator (return).
This article will tell you important information about the assignment operator, its application features, and its main functions. Try to study the material carefully.
Definition of Assignment in Programming
An assignment is ranked as the most frequently used data processing operator. It enters a new value into the variable. The assignment has the following format:
Variable name := expression
The assignment operator is usually executed from right to left. First, the value of the expression is determined and then assigned to the variable. The previous value of the variable is lost, it is replaced by a new one. The value type of the expression must match the value type of the variable. For example, a numeric variable cannot be assigned a text value. An integer variable cannot be assigned a valid value. However, a real variable can be given an integer value, and it will be converted to a real value. For example, an integer value of 5 will be converted to a real value of 5.0.
The practical application of the assignment operator when performing a specific
programming assignment can be difficult. But you can cope with any task if you know the assignment operator algorithm. Find out about it below.
Assignment Operator Algorithm
An assignment operator has two separate parts. The value of the right side expression is evaluated and assigned to the left side variable. It is written in a certain memory location allocated for storing the values of this variable. Therefore, two values correspond to each variable. The first specifies the place for the variable, and the second – is the value itself, which should be written in the place allocated for this variable.
In the program’s implementation, the first value is a memory address. It is also called the left value of a variable because the variable acquires it by being to the left of the assignment sign. The second type of value is used when determining the value of the expression from the right side of the assignment. It is called the right value because of its place in the assignment operator.
Application of the Assignment Operator on the Example of Variables of Any Type
Thanks to the assignment operator, you can assign values to variables of different types, including:
- A variable can be given a specific numerical value. In this case, a numerical constant acts as an expression. For example: and := 1; s := 0;
- A variable can be given the value of another variable, for example, x := y. As a result of the execution of the operator, the variable x acquires a new value, which is equal to the current value of y, that is, the one that the variable y had at the time of assignment;
- The new value of a variable can be determined from its current value. For example, the operator x := x + 1 is used to increase the value of x by one;
- A variable can be assigned a value using a formula. The formula is given by the corresponding expression. For example, to give x a value according to the formula x = pr 2, we should write the assignment operator in the form: x := pi * sqr(r).
Thus, the assignment operator is the easiest to use and most common operator in programming. It is designed to calculate the new value of a particular variable, as well as determine the return value of a function. The assignment operator “:=” is preceded by an operand (variable or address). It gets the value of the expression that follows the assignment operator. The syntax of the assignment operator has the traditional form for programming languages: :=. The variable must be either internal or output.