Go forward to Increment Ops.
Go backward to Boolean Ops.
Go up to Expressions.
Assignment Expressions
======================
An "assignment" is an expression that stores a new value into a
variable. For example, let's assign the value 1 to the variable `z':
z = 1
After this expression is executed, the variable `z' has the value 1.
Whatever old value `z' had before the assignment is forgotten.
Assignments can store string values also. For example, this would
store the value `"this food is good"' in the variable `message':
thing = "food"
predicate = "good"
message = "this " thing " is " predicate
(This also illustrates concatenation of strings.)
The `=' sign is called an "assignment operator". It is the simplest
assignment operator because the value of the right-hand operand is
stored unchanged.
Most operators (addition, concatenation, and so on) have no effect
except to compute a value. If you ignore the value, you might as well
not use the operator. An assignment operator is different; it does
produce a value, but even if you ignore the value, the assignment still
makes itself felt through the alteration of the variable. We call this
a "side effect".
The left-hand operand of an assignment need not be a variable (
see Variables.); it can also be a field (*note Changing the Contents of a
Field: Changing Fields.) or an array element (see Arrays in `awk': Arrays.). These are all called "lvalues", which means they can appear
on the left-hand side of an assignment operator. The right-hand
operand may be any expression; it produces the new value which the
assignment stores in the specified variable, field or array element.
It is important to note that variables do *not* have permanent types.
The type of a variable is simply the type of whatever value it happens
to hold at the moment. In the following program fragment, the variable
`foo' has a numeric value at first, and a string value later on:
foo = 1
print foo
foo = "bar"
print foo
When the second assignment gives `foo' a string value, the fact that it
previously had a numeric value is forgotten.
An assignment is an expression, so it has a value: the same value
that is assigned. Thus, `z = 1' as an expression has the value 1. One
consequence of this is that you can write multiple assignments together:
x = y = z = 0
stores the value 0 in all three variables. It does this because the
value of `z = 0', which is 0, is stored into `y', and then the value of
`y = z = 0', which is 0, is stored into `x'.
You can use an assignment anywhere an expression is called for. For
example, it is valid to write `x != (y = 1)' to set `y' to 1 and then
test whether `x' equals 1. But this style tends to make programs hard
to read; except in a one-shot program, you should rewrite it to get rid
of such nesting of assignments. This is never very hard.
Aside from `=', there are several other assignment operators that do
arithmetic with the old value of the variable. For example, the
operator `+=' computes a new value by adding the right-hand value to
the old value of the variable. Thus, the following assignment adds 5
to the value of `foo':
foo += 5
This is precisely equivalent to the following:
foo = foo + 5
Use whichever one makes the meaning of your program clearer.
Here is a table of the arithmetic assignment operators. In each
case, the right-hand operand is an expression whose value is converted
to a number.
`LVALUE += INCREMENT'
Adds INCREMENT to the value of LVALUE to make the new value of
LVALUE.
`LVALUE -= DECREMENT'
Subtracts DECREMENT from the value of LVALUE.
`LVALUE *= COEFFICIENT'
Multiplies the value of LVALUE by COEFFICIENT.
`LVALUE /= QUOTIENT'
Divides the value of LVALUE by QUOTIENT.
`LVALUE %= MODULUS'
Sets LVALUE to its remainder by MODULUS.
`LVALUE ^= POWER'
`LVALUE **= POWER'
Raises LVALUE to the power POWER. (Only the `^=' operator is
specified by POSIX.)