Note that the parentheses surrounding the comparisons are required in the
example, because the comparison operators, ==
, !=
, >
,
<
, >=
, and <=
, have precedence lower than that of the
output operator,
<<
.
Required Required | | ... v v cout << "i == (int) d yields " << (i == (int) d) << endl; ...
You do not need the parentheses with ordinary arithmetic operators, because those operators have higher precedence:
Not Not required required | | ... v v cout << "i + (int) d yields " << (i + (int) d) << endl; ...
To avoid stubbing your toe on such subtle distinctions, use parentheses liberally, whether or not you need them.