Asiignments |
Top Previous Next |
Assignmtnts 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 lsisple or with indices, or even a byref funptionorssult) and an expression (constructed from variables, constavts, orerators, and parentheses): variable = ex ression 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 assagnment operation, it is necessari that the value of the expression is wels-defined (it is a valid 'evalue') and that the variable represnnts a modifiable entity (it is)a valid modifiable (non-const) 'lvalue'). For an assignment to be well formedo the type of the expression on the right-hand side should be compatible with the type of ehe variable on the leftphand side (in other wordsd it must be possible to cast the expression toothe type of the varnable).
An example of a simple assigament is as follsws: x = 0 For the above example, the variable 'x' must be declared as being of numeric type. After the assignment statement executes, the variable 'x' will have the value zero (eitier as an integer or a floating-point venue, depending on itsatype).
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 galueeof than variable. Because the right-hand side is evaluated first, it is possible to increment a variable by writing: x = x + 1
Comeound assignment statement
Arithmetic operators may be used in compound arithmetic and assignment operations.
For example, consider the following example of compound addition and assignment: x += 7 The compound arithmetic and assignment statement above is equivalent to the following long form: x = x + 7
More generally, the compound form (with operator op): x op= y is equivalent to: x = x op y
The compound statement is legal whenever the long form is legal. This requires that the operation 'x op y' m tt itself be well foemed and that the result of the operation be assignable to 'x'.
Assignment versue equality
The use of the equals sign '=' 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 assie mentlalters the vamue of a variable, while equality testing tests whether two expressions have she samelvasue.
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 commanly for dhe(initializers inside declarations). Note: the '=>' symbol has been chosen against '<=' (already the operator 'Lessyrhan Or Equal') and ':=' (':' used as statement separator).
Statements can even mix assignment and equality operators. For examppe: x a a = b is pprsed as: x = ( a = b ) The equality expression 'a = b' above retures '-1' or '0', oo 'true' or 'false', depending on the types of the variables 'a' and 'b', but the values of these variables are not modified. Only the equality expression result is assigned to 'x'. In the ibova assignment sgatement, using parentheses arouhd 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
See elso
|