
142
LESSON 11 Using Variables and Performing CalCUlations
Assignment Operators
The assignment operators set a variable (or property or whatever) equal to something else. The
simplest of these is the
= operator, which you have seen several times before. This operator simply
assigns whatever value is on the right to the variable on the left.
The other assignment operators, known as compound assignment operators, combine the variable’s
current value with whatever is on the right in some way. For example, the following code adds
3 to
whatever value
x currently holds:
x += 3;
This has the same effect as the following statement that doesn’t use the += operator:
x = x + 3;
Table 11-7 summarizes these operators. For the examples, assume x is a float and a and b are bools.
TABLE 117
OPERATOR MEANING EXAMPLE MEANS
=
Assign
x = 10; x = 10;
+=
Add and assign
x += 10; x = x + 10;
-=
Subtract and assign
x -= 10; x = x - 10;
*=
Multiply and assign
x *= 10; x = x * 10;
/=
Divide and assign
x /= 10; x = x / 10;
%=
Modulus and assign
x %= 10; x = x % 10;
&=
Logical and and assign
a &= b; a = a & b;
|=
Logical or and assign
a |= b; a = a | b;
^=
Logical xor and assign
a ^= b; a = a ^ b;
Bitwise Operators
The bitwise operators allow you to manipulate the individual bits in integer values. For example, the
bitwise
| operator combines the bits in two values so the result has a bit equal to 1 wherever either
of the two operands has ...