zondag 17 juni 2007

The value of an assignment is the left operand!

while ((ch = cin.get()) != EOF)

The value of an assignment is the left operand!

So in the above example, since the parentheses are used, first ch = cin.get() is executed. Since the left operator of an assignment is the value of an assignment, the character read into ch is the value of the expression. That value is then compared with EOF.

And more:
if you wrote:

while (ch = cin.get() != EOF)
The != operator has higher precedence than the = operator.
So first a comparison is made between the return value of cin.get() and EOF, that results in a bool value true or false, now comes the trouble, this bool is then put into ch, not the thing you have expected and wanted at all, so always use parentheses when necessary, and when it makes code clearer to read!

Geen opmerkingen: