We are used to thinking of the four symbols
+
-
*
/
as the only operators.
But other symbols, such as
=
<<
>>
++
--
are also operators.
This means that an entire statement such as
i = j + k;is actually one big expression, consisting of two operators (
+
and
=
)
and three operands
(i
,
j
,
k
).
Some operators do nothing more than compute a value.
An example is the
+
operator when applied to
int
s:
y + x
10 + 20
Other operators also have
side effects.
For example,
=
operator
assigns a new value to a variable:
y = x
<<
operator performs output:
cout << x
>>
operator performs input:
cin >> x
precedence2.C
.
*
(multiplication, line 5)
has higher precedence than
=
(assignment, line 16).
*
(multiplication line 5)
has higher precedence than
<<
(output, line 7).
associativity2.C
.
<<
(output, line 7)
and
>>
(input, line 7)
have left-to-right associativity.
=
(assignment, line 16)
has right-to-left associativity.
See lines 5, 7, and 16 of
this
table.
assignment.C
.
The
assignment operators:
+=
-=
*=
/=
etc.
increment.C
.
The
increment
and
decrement operators:
++
--
(that’s two dashes)