### Assignments

**Assignments**typically allow a variable to hold different values at different times during its life-span and scope.

__Single assignment statement__
An assignment statement consists of a variable (simple or with indices, or even a byref function result) and an expression (constructed from variables, constants, operators, and parentheses):

Executing an assignment statement evaluates the expression on the right-hand side and assigns it to the variable on the left-hand side.

For an assignment operation, it is necessary that the value of the expression is well-defined (it is a valid 'rvalue') and that the variable represents a modifiable entity (it is a valid modifiable (non-const) 'lvalue').

For an assignment to be well formed, the type of the expression on the right-hand side should be compatible with the type of the variable on the left-hand side (in other words, it must be possible to cast the expression to the type of the variable).

An example of a simple assignment is as follows:

For the above example, the variable

After the assignment statement executes, the variable

In an assignment statement:

Because the right-hand side is evaluated first, it is possible to increment a variable by writing:

`variable = expression`For an assignment operation, it is necessary that the value of the expression is well-defined (it is a valid 'rvalue') and that the variable represents a modifiable entity (it is a valid modifiable (non-const) 'lvalue').

For an assignment to be well formed, the type of the expression on the right-hand side should be compatible with the type of the variable on the left-hand side (in other words, it must be possible to cast the expression to the type of the variable).

An example of a simple assignment is as follows:

`x = 0`**must be declared as being of numeric type.**`'x'`After the assignment statement executes, the variable

**will have the value zero (either as an integer or a floating-point value, depending on its type).**`'x'`In an assignment statement:

**-**The expression is evaluated in the current state of the program.

**-**The variable is assigned the computed value, replacing the prior value of that variable.

`x = x + 1`

__Compound assignment statement__
Arithmetic operators may be used in compound arithmetic and assignment operations.

For example, consider the following example of compound addition and assignment:

The compound arithmetic and assignment statement above is equivalent to the following long form:

More generally, the compound form (with operator op):

is equivalent to:

The compound statement is legal whenever the long form is legal. This requires that the operation

For example, consider the following example of compound addition and assignment:

`x += 7`

`x = x + 7`

`x op= y`

`x = x op y`**must itself be well formed and that the result of the operation be assignable to**`'x op y'`**.**`'x'`

__Assignment versus equality__
The use of the equals sign

Beginning programmers sometimes confuse assignment with the relational operator for equality, as

In FreeBASIC, a single equals sign

For this purpose (and for solving some cases of ambiguity of the parser), the alternative symbol

Statements can even mix assignment and equality operators.

For example:

is parsed as:

The equality expression

In the above assignment statement, using parentheses around the equality expression allows to highlight the global behavior.

The alternative symbol

(

**as an assignment operator has been frequently criticized, due to the conflict with equal as comparison for equality. This results both in confusion by novices in writing code, and confusion even by experienced programmers in reading code.**`'='`Beginning programmers sometimes confuse assignment with the relational operator for equality, as

**means equality in mathematics, and is used for assignment in many languages. But assignment alters the value of a variable, while equality testing tests whether two expressions have the same value.**`'='`In FreeBASIC, a single equals sign

**is generally used for both the assignment operator and the equality relational operator, with context determining which is meant.**`'='`For this purpose (and for solving some cases of ambiguity of the parser), the alternative symbol

**can be used for assignments in place of**`'=>'`**(same as already used commonly for the initializers inside declarations).**`'='`**Note:**the**symbol has been chosen against**`'=>'`**(already the operator 'Less Than Or Equal') and**`'<='`**(**`':='`**used as statement separator).**`':'`Statements can even mix assignment and equality operators.

For example:

`x = a = b`

`x = ( a = b )`**above returns**`'a = b'``'-1'``or`**, or**`'0'``'true'``or`**, depending on the types of the variables**`'false'`**and**`'a'`**, but the values of these variables are not modified. Only the equality expression result is assigned to**`'b'`**.**`'x'`In the above assignment statement, using parentheses around the equality expression allows to highlight the global behavior.

The alternative symbol

**can also be used:**`'=>'`

`x => a = b`(

**can not be also used as symbol for equality operator)**

`'=>'`

__Example__
Example illustrating the different cases:

Dim As Integer x, y, z

x = 5 '' (or 'x => 5')

Print x '' 5 (assignment expression is a constant)

y = x + 4 '' (or 'y => x + 4')

Print y '' 9 (assignment expression is the sum of a variable and a constant)

y = y + 3 '' (or 'y => y + 3')

Print y '' 12 (value of x is incremented by 3)

z = 3 '' (or 'z => 3')

z *= x '' (or 'z *=> x')

Print z '' 15 (value of z is multiplied by value of x)

If x = y Then '' (value of x is not modified)

Print x

Else

Print x, y '' 5 12

End If

x = y = z '' (or 'x => y = z') (value of y is not modified)

Print x, y, z '' 0 12 15

x = 5 '' (or 'x => 5')

Print x '' 5 (assignment expression is a constant)

y = x + 4 '' (or 'y => x + 4')

Print y '' 9 (assignment expression is the sum of a variable and a constant)

y = y + 3 '' (or 'y => y + 3')

Print y '' 12 (value of x is incremented by 3)

z = 3 '' (or 'z => 3')

z *= x '' (or 'z *=> x')

Print z '' 15 (value of z is multiplied by value of x)

If x = y Then '' (value of x is not modified)

Print x

Else

Print x, y '' 5 12

End If

x = y = z '' (or 'x => y = z') (value of y is not modified)

Print x, y, z '' 0 12 15

__See also__Back to Programmer's Guide